Skip to content

Commit 9653969

Browse files
authored
Refactor VideoDecoder C++ initialization (#435)
1 parent 2bc0f8c commit 9653969

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -234,59 +234,72 @@ VideoDecoder::VideoDecoder(const void* buffer, size_t length) {
234234
}
235235

236236
void VideoDecoder::initializeDecoder() {
237-
// Some formats don't store enough info in the header so we read/decode a few
238-
// frames to grab that. This is needed for the filter graph. Note: If this
239-
// takes a long time, consider initializing the filter graph after the first
240-
// frame decode.
237+
TORCH_CHECK(!initialized_, "Attempted double initialization.");
238+
241239
int ffmpegStatus = avformat_find_stream_info(formatContext_.get(), nullptr);
242240
if (ffmpegStatus < 0) {
243241
throw std::runtime_error(
244242
"Failed to find stream info: " +
245243
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
246244
}
247-
containerMetadata_.streams.resize(0);
245+
248246
for (int i = 0; i < formatContext_->nb_streams; i++) {
249247
AVStream* stream = formatContext_->streams[i];
250-
containerMetadata_.streams.resize(containerMetadata_.streams.size() + 1);
251-
auto& curr = containerMetadata_.streams.back();
252-
curr.streamIndex = i;
253-
curr.mediaType = stream->codecpar->codec_type;
254-
curr.codecName = avcodec_get_name(stream->codecpar->codec_id);
255-
curr.bitRate = stream->codecpar->bit_rate;
248+
StreamMetadata meta;
249+
250+
TORCH_CHECK(
251+
i == stream->index,
252+
"Our stream index, " + std::to_string(i) +
253+
", does not match AVStream's index, " +
254+
std::to_string(stream->index) + ".");
255+
meta.streamIndex = i;
256+
meta.mediaType = stream->codecpar->codec_type;
257+
meta.codecName = avcodec_get_name(stream->codecpar->codec_id);
258+
meta.bitRate = stream->codecpar->bit_rate;
256259

257260
int64_t frameCount = stream->nb_frames;
258261
if (frameCount > 0) {
259-
curr.numFrames = frameCount;
262+
meta.numFrames = frameCount;
260263
}
264+
261265
if (stream->duration > 0 && stream->time_base.den > 0) {
262-
curr.durationSeconds = av_q2d(stream->time_base) * stream->duration;
266+
meta.durationSeconds = av_q2d(stream->time_base) * stream->duration;
263267
}
268+
264269
double fps = av_q2d(stream->r_frame_rate);
265270
if (fps > 0) {
266-
curr.averageFps = fps;
271+
meta.averageFps = fps;
267272
}
268273

269274
if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
270275
containerMetadata_.numVideoStreams++;
271276
} else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
272277
containerMetadata_.numAudioStreams++;
273278
}
279+
280+
containerMetadata_.streams.push_back(meta);
274281
}
282+
275283
if (formatContext_->duration > 0) {
276284
containerMetadata_.durationSeconds =
277285
ptsToSeconds(formatContext_->duration, AV_TIME_BASE);
278286
}
287+
279288
if (formatContext_->bit_rate > 0) {
280289
containerMetadata_.bitRate = formatContext_->bit_rate;
281290
}
291+
282292
int bestVideoStream = getBestStreamIndex(AVMEDIA_TYPE_VIDEO);
283293
if (bestVideoStream >= 0) {
284294
containerMetadata_.bestVideoStreamIndex = bestVideoStream;
285295
}
296+
286297
int bestAudioStream = getBestStreamIndex(AVMEDIA_TYPE_AUDIO);
287298
if (bestAudioStream >= 0) {
288299
containerMetadata_.bestAudioStreamIndex = bestAudioStream;
289300
}
301+
302+
initialized_ = true;
290303
}
291304

292305
std::unique_ptr<VideoDecoder> VideoDecoder::createFromFilePath(

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ class VideoDecoder {
420420
std::unique_ptr<AVIOBytesContext> ioBytesContext_;
421421
// Whether or not we have already scanned all streams to update the metadata.
422422
bool scanned_all_streams_ = false;
423+
// Tracks that we've already been initialized.
424+
bool initialized_ = false;
423425
};
424426

425427
// --------------------------------------------------------------------------

0 commit comments

Comments
 (0)