Skip to content

Commit eaf3dd3

Browse files
authored
Rename FilterState into FilterGraphContext (#478)
1 parent baa9798 commit eaf3dd3

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,12 @@ void VideoDecoder::createFilterGraph(
337337
StreamInfo& streamInfo,
338338
int expectedOutputHeight,
339339
int expectedOutputWidth) {
340-
FilterState& filterState = streamInfo.filterState;
341-
filterState.filterGraph.reset(avfilter_graph_alloc());
342-
TORCH_CHECK(filterState.filterGraph.get() != nullptr);
340+
FilterGraphContext& filterGraphContext = streamInfo.filterGraphContext;
341+
filterGraphContext.filterGraph.reset(avfilter_graph_alloc());
342+
TORCH_CHECK(filterGraphContext.filterGraph.get() != nullptr);
343343

344344
if (streamInfo.videoStreamOptions.ffmpegThreadCount.has_value()) {
345-
filterState.filterGraph->nb_threads =
345+
filterGraphContext.filterGraph->nb_threads =
346346
streamInfo.videoStreamOptions.ffmpegThreadCount.value();
347347
}
348348

@@ -360,25 +360,25 @@ void VideoDecoder::createFilterGraph(
360360
<< codecContext->sample_aspect_ratio.den;
361361

362362
int ffmpegStatus = avfilter_graph_create_filter(
363-
&filterState.sourceContext,
363+
&filterGraphContext.sourceContext,
364364
buffersrc,
365365
"in",
366366
filterArgs.str().c_str(),
367367
nullptr,
368-
filterState.filterGraph.get());
368+
filterGraphContext.filterGraph.get());
369369
if (ffmpegStatus < 0) {
370370
throw std::runtime_error(
371371
std::string("Failed to create filter graph: ") + filterArgs.str() +
372372
": " + getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
373373
}
374374

375375
ffmpegStatus = avfilter_graph_create_filter(
376-
&filterState.sinkContext,
376+
&filterGraphContext.sinkContext,
377377
buffersink,
378378
"out",
379379
nullptr,
380380
nullptr,
381-
filterState.filterGraph.get());
381+
filterGraphContext.filterGraph.get());
382382
if (ffmpegStatus < 0) {
383383
throw std::runtime_error(
384384
"Failed to create filter graph: " +
@@ -388,7 +388,7 @@ void VideoDecoder::createFilterGraph(
388388
enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE};
389389

390390
ffmpegStatus = av_opt_set_int_list(
391-
filterState.sinkContext,
391+
filterGraphContext.sinkContext,
392392
"pix_fmts",
393393
pix_fmts,
394394
AV_PIX_FMT_NONE,
@@ -403,11 +403,11 @@ void VideoDecoder::createFilterGraph(
403403
UniqueAVFilterInOut inputs(avfilter_inout_alloc());
404404

405405
outputs->name = av_strdup("in");
406-
outputs->filter_ctx = filterState.sourceContext;
406+
outputs->filter_ctx = filterGraphContext.sourceContext;
407407
outputs->pad_idx = 0;
408408
outputs->next = nullptr;
409409
inputs->name = av_strdup("out");
410-
inputs->filter_ctx = filterState.sinkContext;
410+
inputs->filter_ctx = filterGraphContext.sinkContext;
411411
inputs->pad_idx = 0;
412412
inputs->next = nullptr;
413413

@@ -418,7 +418,7 @@ void VideoDecoder::createFilterGraph(
418418
AVFilterInOut* outputsTmp = outputs.release();
419419
AVFilterInOut* inputsTmp = inputs.release();
420420
ffmpegStatus = avfilter_graph_parse_ptr(
421-
filterState.filterGraph.get(),
421+
filterGraphContext.filterGraph.get(),
422422
description.str().c_str(),
423423
&inputsTmp,
424424
&outputsTmp,
@@ -431,7 +431,8 @@ void VideoDecoder::createFilterGraph(
431431
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
432432
}
433433

434-
ffmpegStatus = avfilter_graph_config(filterState.filterGraph.get(), nullptr);
434+
ffmpegStatus =
435+
avfilter_graph_config(filterGraphContext.filterGraph.get(), nullptr);
435436
if (ffmpegStatus < 0) {
436437
throw std::runtime_error(
437438
"Failed to configure filter graph: " +
@@ -1057,7 +1058,7 @@ void VideoDecoder::convertAVFrameToFrameOutputOnCPU(
10571058
} else if (
10581059
streamInfo.colorConversionLibrary ==
10591060
ColorConversionLibrary::FILTERGRAPH) {
1060-
if (!streamInfo.filterState.filterGraph ||
1061+
if (!streamInfo.filterGraphContext.filterGraph ||
10611062
streamInfo.prevFrameContext != frameContext) {
10621063
createFilterGraph(streamInfo, expectedOutputHeight, expectedOutputWidth);
10631064
streamInfo.prevFrameContext = frameContext;
@@ -1615,16 +1616,17 @@ int VideoDecoder::convertAVFrameToTensorUsingSwsScale(
16151616
torch::Tensor VideoDecoder::convertAVFrameToTensorUsingFilterGraph(
16161617
int streamIndex,
16171618
const AVFrame* avFrame) {
1618-
FilterState& filterState = streamInfos_[streamIndex].filterState;
1619+
FilterGraphContext& filterGraphContext =
1620+
streamInfos_[streamIndex].filterGraphContext;
16191621
int ffmpegStatus =
1620-
av_buffersrc_write_frame(filterState.sourceContext, avFrame);
1622+
av_buffersrc_write_frame(filterGraphContext.sourceContext, avFrame);
16211623
if (ffmpegStatus < AVSUCCESS) {
16221624
throw std::runtime_error("Failed to add frame to buffer source context");
16231625
}
16241626

16251627
UniqueAVFrame filteredAVFrame(av_frame_alloc());
1626-
ffmpegStatus =
1627-
av_buffersink_get_frame(filterState.sinkContext, filteredAVFrame.get());
1628+
ffmpegStatus = av_buffersink_get_frame(
1629+
filterGraphContext.sinkContext, filteredAVFrame.get());
16281630
TORCH_CHECK_EQ(filteredAVFrame->format, AV_PIX_FMT_RGB24);
16291631

16301632
auto frameDims = getHeightAndWidthFromResizedAVFrame(*filteredAVFrame.get());

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class VideoDecoder {
293293
int64_t nextPts = INT64_MAX;
294294
};
295295

296-
struct FilterState {
296+
struct FilterGraphContext {
297297
UniqueAVFilterGraph filterGraph;
298298
AVFilterContext* sourceContext = nullptr;
299299
AVFilterContext* sinkContext = nullptr;
@@ -325,7 +325,7 @@ class VideoDecoder {
325325
VideoStreamOptions videoStreamOptions;
326326
// The filter state associated with this stream (for video streams). The
327327
// actual graph will be nullptr for inactive streams.
328-
FilterState filterState;
328+
FilterGraphContext filterGraphContext;
329329
ColorConversionLibrary colorConversionLibrary = FILTERGRAPH;
330330
std::vector<FrameInfo> keyFrames;
331331
std::vector<FrameInfo> allFrames;

0 commit comments

Comments
 (0)