@@ -411,7 +411,8 @@ VideoDecoder::VideoStreamOptions::VideoStreamOptions(
411411void VideoDecoder::addStream (
412412 int streamIndex,
413413 AVMediaType mediaType,
414- const torch::Device& device) {
414+ const torch::Device& device,
415+ std::optional<int > ffmpegThreadCount) {
415416 TORCH_CHECK (
416417 activeStreamIndex_ == NO_ACTIVE_STREAM,
417418 " Can only add one single stream." );
@@ -462,6 +463,8 @@ void VideoDecoder::addStream(
462463 streamInfo.codecContext .get (), streamInfo.stream ->codecpar );
463464 TORCH_CHECK_EQ (retVal, AVSUCCESS);
464465
466+ streamInfo.codecContext ->thread_count = ffmpegThreadCount.value_or (0 );
467+
465468 // TODO_CODE_QUALITY same as above.
466469 if (mediaType == AVMEDIA_TYPE_VIDEO && device.type () == torch::kCUDA ) {
467470 initializeContextOnCuda (device, codecContext);
@@ -495,7 +498,11 @@ void VideoDecoder::addVideoStream(
495498 videoStreamOptions.device .type () == torch::kCUDA ,
496499 " Invalid device type: " + videoStreamOptions.device .str ());
497500
498- addStream (streamIndex, AVMEDIA_TYPE_VIDEO, videoStreamOptions.device );
501+ addStream (
502+ streamIndex,
503+ AVMEDIA_TYPE_VIDEO,
504+ videoStreamOptions.device ,
505+ videoStreamOptions.ffmpegThreadCount );
499506
500507 auto & streamMetadata =
501508 containerMetadata_.allStreamMetadata [activeStreamIndex_];
@@ -510,8 +517,6 @@ void VideoDecoder::addVideoStream(
510517
511518 auto & streamInfo = streamInfos_[activeStreamIndex_];
512519 streamInfo.videoStreamOptions = videoStreamOptions;
513- streamInfo.codecContext ->thread_count =
514- videoStreamOptions.ffmpegThreadCount .value_or (0 );
515520
516521 streamMetadata.width = streamInfo.codecContext ->width ;
517522 streamMetadata.height = streamInfo.codecContext ->height ;
0 commit comments