Skip to content

Attempt to fix FFMPEG 5.0 compatibility #5644

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 26 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 19 additions & 34 deletions torchvision/csrc/io/decoder/decoder.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "decoder.h"
#include <c10/util/Logging.h>
#include <libavutil/avutil.h>
#include <cerrno>
#include <future>
#include <iostream>
#include <mutex>
Expand All @@ -18,25 +19,6 @@ constexpr size_t kIoBufferSize = 96 * 1024;
constexpr size_t kIoPaddingSize = AV_INPUT_BUFFER_PADDING_SIZE;
constexpr size_t kLogBufferSize = 1024;

int ffmpeg_lock(void** mutex, enum AVLockOp op) {
std::mutex** handle = (std::mutex**)mutex;
switch (op) {
case AV_LOCK_CREATE:
*handle = new std::mutex();
break;
case AV_LOCK_OBTAIN:
(*handle)->lock();
break;
case AV_LOCK_RELEASE:
(*handle)->unlock();
break;
case AV_LOCK_DESTROY:
delete *handle;
break;
}
return 0;
}

bool mapFfmpegType(AVMediaType media, MediaType* type) {
switch (media) {
case AVMEDIA_TYPE_AUDIO:
Expand Down Expand Up @@ -202,8 +184,6 @@ void Decoder::initOnce() {
avcodec_register_all();
#endif
avformat_network_init();
// register ffmpeg lock manager
av_lockmgr_register(&ffmpeg_lock);
av_log_set_callback(Decoder::logFunction);
av_log_set_level(AV_LOG_ERROR);
VLOG(1) << "Registered ffmpeg libs";
Expand Down Expand Up @@ -277,7 +257,7 @@ bool Decoder::init(
break;
}

fmt = av_find_input_format(fmtName);
fmt = (AVInputFormat*)av_find_input_format(fmtName);
}

const size_t avioCtxBufferSize = kIoBufferSize;
Expand Down Expand Up @@ -505,10 +485,15 @@ int Decoder::getFrame(size_t workingTimeInMs) {
// once decode() method gets called and grab some bytes
// run this method again
// init package
AVPacket avPacket;
av_init_packet(&avPacket);
avPacket.data = nullptr;
avPacket.size = 0;
// update 03/22: moving memory management to ffmpeg
AVPacket* avPacket;
avPacket = av_packet_alloc();
if (avPacket == nullptr) {
LOG(ERROR) << "decoder as not able to allocate the packet.";
return AVERROR(ENOMEM);
}
avPacket->data = nullptr;
avPacket->size = 0;

auto end = std::chrono::steady_clock::now() +
std::chrono::milliseconds(workingTimeInMs);
Expand All @@ -521,7 +506,7 @@ int Decoder::getFrame(size_t workingTimeInMs) {
size_t decodingErrors = 0;
bool decodedFrame = false;
while (!interrupted_ && inRange_.any() && !decodedFrame && watcher()) {
result = av_read_frame(inputCtx_, &avPacket);
result = av_read_frame(inputCtx_, avPacket);
if (result == AVERROR(EAGAIN)) {
VLOG(4) << "Decoder is busy...";
std::this_thread::yield();
Expand All @@ -538,10 +523,11 @@ int Decoder::getFrame(size_t workingTimeInMs) {
break;
}

// get stream
auto stream = findByIndex(avPacket.stream_index);
// get stream; if stream cannot be found reset the packet to
// default settings
auto stream = findByIndex(avPacket->stream_index);
if (stream == nullptr || !inRange_.test(stream->getIndex())) {
av_packet_unref(&avPacket);
av_packet_unref(avPacket);
continue;
}

Expand All @@ -553,7 +539,7 @@ int Decoder::getFrame(size_t workingTimeInMs) {
bool hasMsg = false;
// packet either got consumed completely or not at all
if ((result = processPacket(
stream, &avPacket, &gotFrame, &hasMsg, params_.fastSeek)) < 0) {
stream, avPacket, &gotFrame, &hasMsg, params_.fastSeek)) < 0) {
LOG(ERROR) << "processPacket failed with code: " << result;
break;
}
Expand Down Expand Up @@ -585,11 +571,10 @@ int Decoder::getFrame(size_t workingTimeInMs) {

result = 0;

av_packet_unref(&avPacket);
av_packet_unref(avPacket);
}

av_packet_unref(&avPacket);

av_packet_free(&avPacket);
VLOG(2) << "Interrupted loop"
<< ", interrupted_ " << interrupted_ << ", inRange_.any() "
<< inRange_.any() << ", decodedFrame " << decodedFrame << ", result "
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/io/decoder/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Stream::~Stream() {

// look up the proper CODEC querying the function
AVCodec* Stream::findCodec(AVCodecParameters* params) {
return avcodec_find_decoder(params->codec_id);
return (AVCodec*)avcodec_find_decoder(params->codec_id);
}

// Allocate memory for the AVCodecContext, which will hold the context for
Expand Down
30 changes: 22 additions & 8 deletions torchvision/csrc/io/decoder/subtitle_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,34 @@ int SubtitleStream::initFormat() {
int SubtitleStream::analyzePacket(const AVPacket* packet, bool* gotFrame) {
// clean-up
releaseSubtitle();

// FIXME: should this even be created?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree - I am also not sure if this should be created. Is the plan to follow up on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I think this would require a bit of coordination on Meta's side, as this is not used nor tested in TV.

AVPacket* avPacket;
avPacket = av_packet_alloc();
if (avPacket == nullptr) {
LOG(ERROR)
<< "decoder as not able to allocate the subtitle-specific packet.";
// alternative to ENOMEM
return AVERROR_BUFFER_TOO_SMALL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we align the error code with the corresponding bit in decoder.cpp

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdsgomes The two methods return different enums. Originally Bruno had them synced; no strong opinions on my side.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdsgomes would you like me to change it to match?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer for consistency, unless there is a reason not to. Other than that the PR looks good and the internal tests are passing. Thank you for these changes

}
avPacket->data = nullptr;
avPacket->size = 0;
// check flush packet
AVPacket avPacket;
av_init_packet(&avPacket);
avPacket.data = nullptr;
avPacket.size = 0;
auto pkt = packet ? *packet : avPacket;
auto pkt = packet ? packet : avPacket;

int gotFramePtr = 0;
int result = avcodec_decode_subtitle2(codecCtx_, &sub_, &gotFramePtr, &pkt);
// is these a better way than cast from const?
int result =
avcodec_decode_subtitle2(codecCtx_, &sub_, &gotFramePtr, (AVPacket*)pkt);

if (result < 0) {
LOG(ERROR) << "avcodec_decode_subtitle2 failed, err: "
<< Util::generateErrorDesc(result);
// free the packet we've created
av_packet_free(&avPacket);
return result;
} else if (result == 0) {
result = pkt.size; // discard the rest of the package
result = pkt->size; // discard the rest of the package
}

sub_.release = gotFramePtr;
Expand All @@ -66,9 +79,10 @@ int SubtitleStream::analyzePacket(const AVPacket* packet, bool* gotFrame) {
// set proper pts in us
if (gotFramePtr) {
sub_.pts = av_rescale_q(
pkt.pts, inputCtx_->streams[format_.stream]->time_base, timeBaseQ);
pkt->pts, inputCtx_->streams[format_.stream]->time_base, timeBaseQ);
}

av_packet_free(&avPacket);
return result;
}

Expand Down