@@ -198,10 +198,12 @@ VideoDecoder::BatchDecodedOutput::BatchDecodedOutput(
198198 const StreamMetadata& streamMetadata)
199199 : ptsSeconds(torch::empty({numFrames}, {torch::kFloat64 })),
200200 durationSeconds (torch::empty({numFrames}, {torch::kFloat64 })) {
201- auto frameDims = getHeightAndWidthFromOptionsOrMetadata (videoStreamOptions, streamMetadata);
201+ auto frameDims = getHeightAndWidthFromOptionsOrMetadata (
202+ videoStreamOptions, streamMetadata);
202203 int height = frameDims.height ;
203204 int width = frameDims.width ;
204- frames = allocateEmptyHWCTensor (height, width, videoStreamOptions.device , numFrames);
205+ frames = allocateEmptyHWCTensor (
206+ height, width, videoStreamOptions.device , numFrames);
205207}
206208
207209bool VideoDecoder::DecodedFrameContext::operator ==(
@@ -272,7 +274,8 @@ void VideoDecoder::initializeDecoder() {
272274 }
273275
274276 if (avStream->duration > 0 && avStream->time_base .den > 0 ) {
275- streamMetadata.durationSeconds = av_q2d (avStream->time_base ) * avStream->duration ;
277+ streamMetadata.durationSeconds =
278+ av_q2d (avStream->time_base ) * avStream->duration ;
276279 }
277280
278281 double fps = av_q2d (avStream->r_frame_rate );
@@ -465,7 +468,8 @@ void VideoDecoder::addVideoStreamDecoder(
465468 }
466469 TORCH_CHECK (codec != nullptr );
467470
468- StreamMetadata& streamMetadata = containerMetadata_.streamMetadatas [streamIndex];
471+ StreamMetadata& streamMetadata =
472+ containerMetadata_.streamMetadatas [streamIndex];
469473 if (seekMode_ == SeekMode::approximate &&
470474 !streamMetadata.averageFps .has_value ()) {
471475 throw std::runtime_error (
@@ -485,8 +489,10 @@ void VideoDecoder::addVideoStreamDecoder(
485489 }
486490
487491 if (videoStreamOptions.device .type () == torch::kCUDA ) {
488- codec = findCudaCodec (videoStreamOptions.device , streamInfo.stream ->codecpar ->codec_id )
489- .value_or (codec);
492+ codec =
493+ findCudaCodec (
494+ videoStreamOptions.device , streamInfo.stream ->codecpar ->codec_id )
495+ .value_or (codec);
490496 }
491497
492498 AVCodecContext* codecContext = avcodec_alloc_context3 (codec);
@@ -503,7 +509,8 @@ void VideoDecoder::addVideoStreamDecoder(
503509 } else if (videoStreamOptions.device .type () == torch::kCUDA ) {
504510 initializeContextOnCuda (videoStreamOptions.device , codecContext);
505511 } else {
506- TORCH_CHECK (false , " Invalid device type: " + videoStreamOptions.device .str ());
512+ TORCH_CHECK (
513+ false , " Invalid device type: " + videoStreamOptions.device .str ());
507514 }
508515
509516 retVal = avcodec_open2 (streamInfo.codecContext .get (), codec, nullptr );
@@ -614,12 +621,14 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
614621
615622 // Set all per-stream metadata that requires knowing the content of all
616623 // packets.
617- for (size_t streamIndex = 0 ; streamIndex < containerMetadata_.streamMetadatas .size ();
624+ for (size_t streamIndex = 0 ;
625+ streamIndex < containerMetadata_.streamMetadatas .size ();
618626 ++streamIndex) {
619627 auto & streamMetadata = containerMetadata_.streamMetadatas [streamIndex];
620628 auto avStream = formatContext_->streams [streamIndex];
621629
622- streamMetadata.numFramesFromScan = streamInfos_[streamIndex].allFrames .size ();
630+ streamMetadata.numFramesFromScan =
631+ streamInfos_[streamIndex].allFrames .size ();
623632
624633 if (streamMetadata.minPtsFromScan .has_value ()) {
625634 streamMetadata.minPtsSecondsFromScan =
@@ -915,8 +924,8 @@ VideoDecoder::DecodedOutput VideoDecoder::convertAVFrameToDecodedOutput(
915924 output.streamIndex = streamIndex;
916925 auto & streamInfo = streamInfos_[streamIndex];
917926 TORCH_CHECK (streamInfo.stream ->codecpar ->codec_type == AVMEDIA_TYPE_VIDEO);
918- output.ptsSeconds =
919- ptsToSeconds ( avFrame->pts , formatContext_->streams [streamIndex]->time_base );
927+ output.ptsSeconds = ptsToSeconds (
928+ avFrame->pts , formatContext_->streams [streamIndex]->time_base );
920929 output.durationSeconds = ptsToSeconds (
921930 getDuration (avFrame), formatContext_->streams [streamIndex]->time_base );
922931 // TODO: we should fold preAllocatedOutputTensor into RawDecodedOutput.
@@ -932,7 +941,8 @@ VideoDecoder::DecodedOutput VideoDecoder::convertAVFrameToDecodedOutput(
932941 preAllocatedOutputTensor);
933942 } else {
934943 TORCH_CHECK (
935- false , " Invalid device type: " + streamInfo.videoStreamOptions .device .str ());
944+ false ,
945+ " Invalid device type: " + streamInfo.videoStreamOptions .device .str ());
936946 }
937947 return output;
938948}
@@ -954,8 +964,8 @@ void VideoDecoder::convertAVFrameToDecodedOutputOnCPU(
954964 AVFrame* avFrame = rawOutput.avFrame .get ();
955965 auto & streamInfo = streamInfos_[streamIndex];
956966
957- auto frameDims =
958- getHeightAndWidthFromOptionsOrAVFrame ( streamInfo.videoStreamOptions , *avFrame);
967+ auto frameDims = getHeightAndWidthFromOptionsOrAVFrame (
968+ streamInfo.videoStreamOptions , *avFrame);
959969 int expectedOutputHeight = frameDims.height ;
960970 int expectedOutputWidth = frameDims.width ;
961971
@@ -1051,9 +1061,11 @@ void VideoDecoder::convertAVFrameToDecodedOutputOnCPU(
10511061VideoDecoder::DecodedOutput VideoDecoder::getFramePlayedAtTimestampNoDemux (
10521062 double seconds) {
10531063 for (auto & [streamIndex, streamInfo] : streamInfos_) {
1054- double frameStartTime = ptsToSeconds (streamInfo.currentPts , streamInfo.timeBase );
1064+ double frameStartTime =
1065+ ptsToSeconds (streamInfo.currentPts , streamInfo.timeBase );
10551066 double frameEndTime = ptsToSeconds (
1056- streamInfo.currentPts + streamInfo.currentDuration , streamInfo.timeBase );
1067+ streamInfo.currentPts + streamInfo.currentDuration ,
1068+ streamInfo.timeBase );
10571069 if (seconds >= frameStartTime && seconds < frameEndTime) {
10581070 // We are in the same frame as the one we just returned. However, since we
10591071 // don't cache it locally, we have to rewind back.
@@ -1067,8 +1079,8 @@ VideoDecoder::DecodedOutput VideoDecoder::getFramePlayedAtTimestampNoDemux(
10671079 [seconds, this ](int frameStreamIndex, AVFrame* avFrame) {
10681080 StreamInfo& streamInfo = streamInfos_[frameStreamIndex];
10691081 double frameStartTime = ptsToSeconds (avFrame->pts , streamInfo.timeBase );
1070- double frameEndTime =
1071- ptsToSeconds ( avFrame->pts + getDuration (avFrame), streamInfo.timeBase );
1082+ double frameEndTime = ptsToSeconds (
1083+ avFrame->pts + getDuration (avFrame), streamInfo.timeBase );
10721084 if (frameStartTime > seconds) {
10731085 // FFMPEG seeked past the frame we are looking for even though we
10741086 // set max_ts to be our needed timestamp in avformat_seek_file()
@@ -1263,7 +1275,8 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
12631275 const auto & streamMetadata = containerMetadata_.streamMetadatas [streamIndex];
12641276 const auto & streamInfo = streamInfos_[streamIndex];
12651277 const auto & videoStreamOptions = streamInfo.videoStreamOptions ;
1266- BatchDecodedOutput output (frameIndices.size (), videoStreamOptions, streamMetadata);
1278+ BatchDecodedOutput output (
1279+ frameIndices.size (), videoStreamOptions, streamMetadata);
12671280
12681281 auto previousIndexInVideo = -1 ;
12691282 for (size_t f = 0 ; f < frameIndices.size (); ++f) {
@@ -1345,7 +1358,8 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesInRange(
13451358
13461359 int64_t numOutputFrames = std::ceil ((stop - start) / double (step));
13471360 const auto & videoStreamOptions = streamInfo.videoStreamOptions ;
1348- BatchDecodedOutput output (numOutputFrames, videoStreamOptions, streamMetadata);
1361+ BatchDecodedOutput output (
1362+ numOutputFrames, videoStreamOptions, streamMetadata);
13491363
13501364 for (int64_t i = start, f = 0 ; i < stop; i += step, ++f) {
13511365 DecodedOutput singleOut =
@@ -1442,8 +1456,8 @@ VideoDecoder::getFramesPlayedByTimestampInRange(
14421456}
14431457
14441458VideoDecoder::RawDecodedOutput VideoDecoder::getNextRawDecodedOutputNoDemux () {
1445- auto rawOutput =
1446- getDecodedOutputWithFilter ( [this ](int frameStreamIndex, AVFrame* avFrame) {
1459+ auto rawOutput = getDecodedOutputWithFilter (
1460+ [this ](int frameStreamIndex, AVFrame* avFrame) {
14471461 StreamInfo& activeStreamInfo = streamInfos_[frameStreamIndex];
14481462 return avFrame->pts >= activeStreamInfo.discardFramesBeforePts ;
14491463 });
@@ -1559,7 +1573,8 @@ torch::Tensor VideoDecoder::convertAVFrameToTensorUsingFilterGraph(
15591573 int streamIndex,
15601574 const AVFrame* avFrame) {
15611575 FilterState& filterState = streamInfos_[streamIndex].filterState ;
1562- int ffmpegStatus = av_buffersrc_write_frame (filterState.sourceContext , avFrame);
1576+ int ffmpegStatus =
1577+ av_buffersrc_write_frame (filterState.sourceContext , avFrame);
15631578 if (ffmpegStatus < AVSUCCESS) {
15641579 throw std::runtime_error (" Failed to add frame to buffer source context" );
15651580 }
0 commit comments