Skip to content

Commit da9164e

Browse files
authored
Refactor ffmpegStatus to status (#534)
1 parent 5e325a2 commit da9164e

File tree

1 file changed

+49
-52
lines changed

1 file changed

+49
-52
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ VideoDecoder::VideoDecoder(const std::string& videoFilePath, SeekMode seekMode)
6868
av_log_set_level(AV_LOG_QUIET);
6969

7070
AVFormatContext* rawContext = nullptr;
71-
int ffmpegStatus =
71+
int status =
7272
avformat_open_input(&rawContext, videoFilePath.c_str(), nullptr, nullptr);
7373
TORCH_CHECK(
74-
ffmpegStatus == 0,
74+
status == 0,
7575
"Could not open input file: " + videoFilePath + " " +
76-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
76+
getFFMPEGErrorStringFromErrorCode(status));
7777
TORCH_CHECK(rawContext != nullptr);
7878
formatContext_.reset(rawContext);
7979

@@ -97,14 +97,13 @@ VideoDecoder::VideoDecoder(const void* data, size_t length, SeekMode seekMode)
9797
TORCH_CHECK(rawContext != nullptr, "Unable to alloc avformat context");
9898

9999
rawContext->pb = ioBytesContext_->getAVIO();
100-
int ffmpegStatus =
101-
avformat_open_input(&rawContext, nullptr, nullptr, nullptr);
102-
if (ffmpegStatus != 0) {
100+
int status = avformat_open_input(&rawContext, nullptr, nullptr, nullptr);
101+
if (status != 0) {
103102
avformat_free_context(rawContext);
104103
TORCH_CHECK(
105104
false,
106105
"Failed to open input buffer: " +
107-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
106+
getFFMPEGErrorStringFromErrorCode(status));
108107
}
109108

110109
formatContext_.reset(rawContext);
@@ -132,11 +131,11 @@ void VideoDecoder::initializeDecoder() {
132131
// store enough info in the header, so we call avformat_find_stream_info()
133132
// which decodes a few frames to get missing info. For more, see:
134133
// https://ffmpeg.org/doxygen/7.0/group__lavf__decoding.html
135-
int ffmpegStatus = avformat_find_stream_info(formatContext_.get(), nullptr);
136-
if (ffmpegStatus < 0) {
134+
int status = avformat_find_stream_info(formatContext_.get(), nullptr);
135+
if (status < 0) {
137136
throw std::runtime_error(
138137
"Failed to find stream info: " +
139-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
138+
getFFMPEGErrorStringFromErrorCode(status));
140139
}
141140

142141
for (unsigned int i = 0; i < formatContext_->nb_streams; i++) {
@@ -231,16 +230,16 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
231230
ReferenceAVPacket packet(autoAVPacket);
232231

233232
// av_read_frame is a misleading name: it gets the next **packet**.
234-
int ffmpegStatus = av_read_frame(formatContext_.get(), packet.get());
233+
int status = av_read_frame(formatContext_.get(), packet.get());
235234

236-
if (ffmpegStatus == AVERROR_EOF) {
235+
if (status == AVERROR_EOF) {
237236
break;
238237
}
239238

240-
if (ffmpegStatus != AVSUCCESS) {
239+
if (status != AVSUCCESS) {
241240
throw std::runtime_error(
242241
"Failed to read frame from input file: " +
243-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
242+
getFFMPEGErrorStringFromErrorCode(status));
244243
}
245244

246245
if (packet->flags & AV_PKT_FLAG_DISCARD) {
@@ -923,23 +922,23 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
923922
// Need to get the next frame or error from PopFrame.
924923
UniqueAVFrame avFrame(av_frame_alloc());
925924
AutoAVPacket autoAVPacket;
926-
int ffmpegStatus = AVSUCCESS;
925+
int status = AVSUCCESS;
927926
bool reachedEOF = false;
928927
while (true) {
929-
ffmpegStatus =
928+
status =
930929
avcodec_receive_frame(streamInfo.codecContext.get(), avFrame.get());
931930

932-
if (ffmpegStatus != AVSUCCESS && ffmpegStatus != AVERROR(EAGAIN)) {
931+
if (status != AVSUCCESS && status != AVERROR(EAGAIN)) {
933932
// Non-retriable error
934933
break;
935934
}
936935

937936
decodeStats_.numFramesReceivedByDecoder++;
938937
// Is this the kind of frame we're looking for?
939-
if (ffmpegStatus == AVSUCCESS && filterFunction(avFrame.get())) {
938+
if (status == AVSUCCESS && filterFunction(avFrame.get())) {
940939
// Yes, this is the frame we'll return; break out of the decoding loop.
941940
break;
942-
} else if (ffmpegStatus == AVSUCCESS) {
941+
} else if (status == AVSUCCESS) {
943942
// No, but we received a valid frame - just not the kind we're looking
944943
// for. The logic below will read packets and send them to the decoder.
945944
// But since we did just receive a frame, we should skip reading more
@@ -958,29 +957,29 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
958957
// packets and send them to the decoder.
959958
ReferenceAVPacket packet(autoAVPacket);
960959
do {
961-
ffmpegStatus = av_read_frame(formatContext_.get(), packet.get());
960+
status = av_read_frame(formatContext_.get(), packet.get());
962961
decodeStats_.numPacketsRead++;
963962

964-
if (ffmpegStatus == AVERROR_EOF) {
963+
if (status == AVERROR_EOF) {
965964
// End of file reached. We must drain the codec by sending a nullptr
966965
// packet.
967-
ffmpegStatus = avcodec_send_packet(
966+
status = avcodec_send_packet(
968967
streamInfo.codecContext.get(),
969968
/*avpkt=*/nullptr);
970-
if (ffmpegStatus < AVSUCCESS) {
969+
if (status < AVSUCCESS) {
971970
throw std::runtime_error(
972971
"Could not flush decoder: " +
973-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
972+
getFFMPEGErrorStringFromErrorCode(status));
974973
}
975974

976975
reachedEOF = true;
977976
break;
978977
}
979978

980-
if (ffmpegStatus < AVSUCCESS) {
979+
if (status < AVSUCCESS) {
981980
throw std::runtime_error(
982981
"Could not read frame from input file: " +
983-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
982+
getFFMPEGErrorStringFromErrorCode(status));
984983
}
985984
} while (packet->stream_index != activeStreamIndex_);
986985

@@ -992,26 +991,25 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
992991

993992
// We got a valid packet. Send it to the decoder, and we'll receive it in
994993
// the next iteration.
995-
ffmpegStatus =
996-
avcodec_send_packet(streamInfo.codecContext.get(), packet.get());
997-
if (ffmpegStatus < AVSUCCESS) {
994+
status = avcodec_send_packet(streamInfo.codecContext.get(), packet.get());
995+
if (status < AVSUCCESS) {
998996
throw std::runtime_error(
999997
"Could not push packet to decoder: " +
1000-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
998+
getFFMPEGErrorStringFromErrorCode(status));
1001999
}
10021000

10031001
decodeStats_.numPacketsSentToDecoder++;
10041002
}
10051003

1006-
if (ffmpegStatus < AVSUCCESS) {
1007-
if (reachedEOF || ffmpegStatus == AVERROR_EOF) {
1004+
if (status < AVSUCCESS) {
1005+
if (reachedEOF || status == AVERROR_EOF) {
10081006
throw VideoDecoder::EndOfFileException(
10091007
"Requested next frame while there are no more frames left to "
10101008
"decode.");
10111009
}
10121010
throw std::runtime_error(
10131011
"Could not receive frame from decoder: " +
1014-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
1012+
getFFMPEGErrorStringFromErrorCode(status));
10151013
}
10161014

10171015
// Note that we don't flush the decoder when we reach EOF (even though that's
@@ -1197,14 +1195,14 @@ torch::Tensor VideoDecoder::convertAVFrameToTensorUsingFilterGraph(
11971195
const AVFrame* avFrame) {
11981196
FilterGraphContext& filterGraphContext =
11991197
streamInfos_[activeStreamIndex_].filterGraphContext;
1200-
int ffmpegStatus =
1198+
int status =
12011199
av_buffersrc_write_frame(filterGraphContext.sourceContext, avFrame);
1202-
if (ffmpegStatus < AVSUCCESS) {
1200+
if (status < AVSUCCESS) {
12031201
throw std::runtime_error("Failed to add frame to buffer source context");
12041202
}
12051203

12061204
UniqueAVFrame filteredAVFrame(av_frame_alloc());
1207-
ffmpegStatus = av_buffersink_get_frame(
1205+
status = av_buffersink_get_frame(
12081206
filterGraphContext.sinkContext, filteredAVFrame.get());
12091207
TORCH_CHECK_EQ(filteredAVFrame->format, AV_PIX_FMT_RGB24);
12101208

@@ -1328,44 +1326,44 @@ void VideoDecoder::createFilterGraph(
13281326
filterArgs << ":pixel_aspect=" << codecContext->sample_aspect_ratio.num << "/"
13291327
<< codecContext->sample_aspect_ratio.den;
13301328

1331-
int ffmpegStatus = avfilter_graph_create_filter(
1329+
int status = avfilter_graph_create_filter(
13321330
&filterGraphContext.sourceContext,
13331331
buffersrc,
13341332
"in",
13351333
filterArgs.str().c_str(),
13361334
nullptr,
13371335
filterGraphContext.filterGraph.get());
1338-
if (ffmpegStatus < 0) {
1336+
if (status < 0) {
13391337
throw std::runtime_error(
13401338
std::string("Failed to create filter graph: ") + filterArgs.str() +
1341-
": " + getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
1339+
": " + getFFMPEGErrorStringFromErrorCode(status));
13421340
}
13431341

1344-
ffmpegStatus = avfilter_graph_create_filter(
1342+
status = avfilter_graph_create_filter(
13451343
&filterGraphContext.sinkContext,
13461344
buffersink,
13471345
"out",
13481346
nullptr,
13491347
nullptr,
13501348
filterGraphContext.filterGraph.get());
1351-
if (ffmpegStatus < 0) {
1349+
if (status < 0) {
13521350
throw std::runtime_error(
13531351
"Failed to create filter graph: " +
1354-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
1352+
getFFMPEGErrorStringFromErrorCode(status));
13551353
}
13561354

13571355
enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE};
13581356

1359-
ffmpegStatus = av_opt_set_int_list(
1357+
status = av_opt_set_int_list(
13601358
filterGraphContext.sinkContext,
13611359
"pix_fmts",
13621360
pix_fmts,
13631361
AV_PIX_FMT_NONE,
13641362
AV_OPT_SEARCH_CHILDREN);
1365-
if (ffmpegStatus < 0) {
1363+
if (status < 0) {
13661364
throw std::runtime_error(
13671365
"Failed to set output pixel formats: " +
1368-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
1366+
getFFMPEGErrorStringFromErrorCode(status));
13691367
}
13701368

13711369
UniqueAVFilterInOut outputs(avfilter_inout_alloc());
@@ -1386,26 +1384,25 @@ void VideoDecoder::createFilterGraph(
13861384

13871385
AVFilterInOut* outputsTmp = outputs.release();
13881386
AVFilterInOut* inputsTmp = inputs.release();
1389-
ffmpegStatus = avfilter_graph_parse_ptr(
1387+
status = avfilter_graph_parse_ptr(
13901388
filterGraphContext.filterGraph.get(),
13911389
description.str().c_str(),
13921390
&inputsTmp,
13931391
&outputsTmp,
13941392
nullptr);
13951393
outputs.reset(outputsTmp);
13961394
inputs.reset(inputsTmp);
1397-
if (ffmpegStatus < 0) {
1395+
if (status < 0) {
13981396
throw std::runtime_error(
13991397
"Failed to parse filter description: " +
1400-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
1398+
getFFMPEGErrorStringFromErrorCode(status));
14011399
}
14021400

1403-
ffmpegStatus =
1404-
avfilter_graph_config(filterGraphContext.filterGraph.get(), nullptr);
1405-
if (ffmpegStatus < 0) {
1401+
status = avfilter_graph_config(filterGraphContext.filterGraph.get(), nullptr);
1402+
if (status < 0) {
14061403
throw std::runtime_error(
14071404
"Failed to configure filter graph: " +
1408-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
1405+
getFFMPEGErrorStringFromErrorCode(status));
14091406
}
14101407
}
14111408

0 commit comments

Comments
 (0)