@@ -234,59 +234,72 @@ VideoDecoder::VideoDecoder(const void* buffer, size_t length) {
234234}
235235
236236void 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
292305std::unique_ptr<VideoDecoder> VideoDecoder::createFromFilePath (
0 commit comments