-
Notifications
You must be signed in to change notification settings - Fork 75
Update Video Encoder and tests for 6 container formats #913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
56a96fb
check in testsrc mp4 and all_frames info
f09fe35
Use testsrc in round trip test
5c94fda
add func to access pix_fmts in FFmpeg7+
f438729
fix 6 container fmts
1162beb
pass crf variable in custom ops
ea85cfe
Add tolerances for various cases
f0fffca
clean up logging, comments
444254e
delete unused resource
4bac987
remove whitespace
75c5b36
adjust test order
796499e
remove positional arg
2cafa10
Make crf optional
1aebfec
crf default
49d85d6
name args?
4ab1b63
windows + webm test skips
5f2928f
compare against cli, high % match
2055291
skip webm/windows/ffmpeg6,7
e0e456c
test gif against ffmpeg cli
d2b2f14
more sensible qscale lower bound
d7bb786
incorporate comment suggestions in test_ops
266f9f5
move torchaudio comment, remove streamIndex_
4516b35
remove qscale, adjust tests
163c5c2
specify cpu in tests
d4017bd
remove outdated test condition
185c656
decrement cli accuracy requirement for avi
4cc2eb3
Revert "specify cpu in tests"
b13b5db
replace in with ==
160d449
use nullptr instead of 0
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,10 @@ | |||||
| #include "src/torchcodec/_core/Encoder.h" | ||||||
| #include "torch/types.h" | ||||||
|
|
||||||
| extern "C" { | ||||||
| #include <libavutil/pixdesc.h> | ||||||
| } | ||||||
|
|
||||||
| namespace facebook::torchcodec { | ||||||
|
|
||||||
| namespace { | ||||||
|
|
@@ -587,15 +591,6 @@ void VideoEncoder::initializeEncoder( | |||||
| TORCH_CHECK(avCodecContext != nullptr, "Couldn't allocate codec context."); | ||||||
| avCodecContext_.reset(avCodecContext); | ||||||
|
|
||||||
| // Set encoding options | ||||||
| // TODO-VideoEncoder: Allow bitrate to be set | ||||||
| std::optional<int> desiredBitRate = videoStreamOptions.bitRate; | ||||||
| if (desiredBitRate.has_value()) { | ||||||
| TORCH_CHECK( | ||||||
| *desiredBitRate >= 0, "bit_rate=", *desiredBitRate, " must be >= 0."); | ||||||
| } | ||||||
| avCodecContext_->bit_rate = desiredBitRate.value_or(0); | ||||||
|
|
||||||
| // Store dimension order and input pixel format | ||||||
| // TODO-VideoEncoder: Remove assumption that tensor in NCHW format | ||||||
| auto sizes = frames_.sizes(); | ||||||
|
|
@@ -608,9 +603,15 @@ void VideoEncoder::initializeEncoder( | |||||
| outWidth_ = inWidth_; | ||||||
| outHeight_ = inHeight_; | ||||||
|
|
||||||
| // Use YUV420P as default output format | ||||||
| // TODO-VideoEncoder: Enable other pixel formats | ||||||
| outPixelFormat_ = AV_PIX_FMT_YUV420P; | ||||||
| // Let FFmpeg choose best pixel format to minimize loss | ||||||
| outPixelFormat_ = avcodec_find_best_pix_fmt_of_list( | ||||||
| getSupportedPixelFormats(*avCodec), // List of supported formats | ||||||
| AV_PIX_FMT_GBRP, // We reorder input to GBRP currently | ||||||
| 0, // No alpha channel | ||||||
| nullptr // Discard conversion loss information | ||||||
| ); | ||||||
| TORCH_CHECK(outPixelFormat_ != -1, "Failed to find best pix fmt") | ||||||
|
|
||||||
| // Configure codec parameters | ||||||
| avCodecContext_->codec_id = avCodec->id; | ||||||
|
|
@@ -621,37 +622,39 @@ void VideoEncoder::initializeEncoder( | |||||
| avCodecContext_->time_base = {1, inFrameRate_}; | ||||||
| avCodecContext_->framerate = {inFrameRate_, 1}; | ||||||
|
|
||||||
| // TODO-VideoEncoder: Allow GOP size and max B-frames to be set | ||||||
| if (videoStreamOptions.gopSize.has_value()) { | ||||||
| avCodecContext_->gop_size = *videoStreamOptions.gopSize; | ||||||
| } else { | ||||||
| avCodecContext_->gop_size = 12; // Default GOP size | ||||||
| // Set flag for containers that require extradata to be in the codec context | ||||||
| if (avFormatContext_->oformat->flags & AVFMT_GLOBALHEADER) { | ||||||
| avCodecContext_->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; | ||||||
| } | ||||||
|
|
||||||
| if (videoStreamOptions.maxBFrames.has_value()) { | ||||||
| avCodecContext_->max_b_frames = *videoStreamOptions.maxBFrames; | ||||||
| } else { | ||||||
| avCodecContext_->max_b_frames = 0; // No max B-frames to reduce compression | ||||||
| // Apply videoStreamOptions | ||||||
| AVDictionary* options = nullptr; | ||||||
| if (videoStreamOptions.crf.has_value()) { | ||||||
| av_dict_set( | ||||||
| &options, | ||||||
| "crf", | ||||||
| std::to_string(videoStreamOptions.crf.value()).c_str(), | ||||||
| 0); | ||||||
| } | ||||||
| int status = avcodec_open2(avCodecContext_.get(), avCodec, &options); | ||||||
| av_dict_free(&options); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is OK, if we start using torchcodec/src/torchcodec/_core/FFMPEGCommon.h Lines 74 to 75 in e5b2eef
|
||||||
|
|
||||||
| int status = avcodec_open2(avCodecContext_.get(), avCodec, nullptr); | ||||||
| TORCH_CHECK( | ||||||
| status == AVSUCCESS, | ||||||
| "avcodec_open2 failed: ", | ||||||
| getFFMPEGErrorStringFromErrorCode(status)); | ||||||
|
|
||||||
| AVStream* avStream = avformat_new_stream(avFormatContext_.get(), nullptr); | ||||||
| TORCH_CHECK(avStream != nullptr, "Couldn't create new stream."); | ||||||
| avStream_ = avformat_new_stream(avFormatContext_.get(), nullptr); | ||||||
| TORCH_CHECK(avStream_ != nullptr, "Couldn't create new stream."); | ||||||
|
|
||||||
| // Set the stream time base to encode correct frame timestamps | ||||||
| avStream->time_base = avCodecContext_->time_base; | ||||||
| avStream_->time_base = avCodecContext_->time_base; | ||||||
| status = avcodec_parameters_from_context( | ||||||
| avStream->codecpar, avCodecContext_.get()); | ||||||
| avStream_->codecpar, avCodecContext_.get()); | ||||||
| TORCH_CHECK( | ||||||
| status == AVSUCCESS, | ||||||
| "avcodec_parameters_from_context failed: ", | ||||||
| getFFMPEGErrorStringFromErrorCode(status)); | ||||||
| streamIndex_ = avStream->index; | ||||||
| } | ||||||
|
|
||||||
| void VideoEncoder::encode() { | ||||||
|
|
@@ -694,7 +697,7 @@ UniqueAVFrame VideoEncoder::convertTensorToAVFrame( | |||||
| outWidth_, | ||||||
| outHeight_, | ||||||
| outPixelFormat_, | ||||||
| SWS_BILINEAR, | ||||||
| SWS_BICUBIC, // Used by FFmpeg CLI | ||||||
| nullptr, | ||||||
| nullptr, | ||||||
| nullptr)); | ||||||
|
|
@@ -757,7 +760,7 @@ void VideoEncoder::encodeFrame( | |||||
| "Error while sending frame: ", | ||||||
| getFFMPEGErrorStringFromErrorCode(status)); | ||||||
|
|
||||||
| while (true) { | ||||||
| while (status >= 0) { | ||||||
| ReferenceAVPacket packet(autoAVPacket); | ||||||
| status = avcodec_receive_packet(avCodecContext_.get(), packet.get()); | ||||||
| if (status == AVERROR(EAGAIN) || status == AVERROR_EOF) { | ||||||
|
|
@@ -776,7 +779,16 @@ void VideoEncoder::encodeFrame( | |||||
| "Error receiving packet: ", | ||||||
| getFFMPEGErrorStringFromErrorCode(status)); | ||||||
|
|
||||||
| packet->stream_index = streamIndex_; | ||||||
| // The code below is borrowed from torchaudio: | ||||||
| // https://github.com/pytorch/audio/blob/b6a3368a45aaafe05f1a6a9f10c68adc5e944d9e/src/libtorio/ffmpeg/stream_writer/encoder.cpp#L46 | ||||||
| // Setting packet->duration to 1 allows the last frame to be properly | ||||||
| // encoded, and needs to be set before calling av_packet_rescale_ts. | ||||||
| if (packet->duration == 0) { | ||||||
| packet->duration = 1; | ||||||
| } | ||||||
| av_packet_rescale_ts( | ||||||
| packet.get(), avCodecContext_->time_base, avStream_->time_base); | ||||||
| packet->stream_index = avStream_->index; | ||||||
|
|
||||||
| status = av_interleaved_write_frame(avFormatContext_.get(), packet.get()); | ||||||
| TORCH_CHECK( | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.