Skip to content
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

Additional fixes for FFmpeg 3.1+ #14188

Merged
merged 3 commits into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions Core/HLE/sceAtrac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,27 @@ struct Atrac {
}

int got_frame = 0;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
if (packet_->size != 0) {
int err = avcodec_send_packet(codecCtx_, packet_);
if (err < 0) {
ERROR_LOG_REPORT(ME, "avcodec_send_packet: Error decoding audio %d / %08x", err, err);
failedDecode_ = true;
return ATDECODE_FAILED;
}
}

int err = avcodec_receive_frame(codecCtx_, frame_);
int bytes_read = 0;
if (err >= 0) {
bytes_read = frame_->pkt_size;
got_frame = 1;
} else if (err != AVERROR(EAGAIN)) {
bytes_read = err;
}
#else
int bytes_read = avcodec_decode_audio4(codecCtx_, frame_, &got_frame, packet_);
#endif
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
av_packet_unref(packet_);
#else
Expand Down
3 changes: 2 additions & 1 deletion Core/HLE/sceMpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,8 @@ static bool decodePmpVideo(PSPPointer<SceMpegRingBuffer> ringbuffer, u32 pmpctxA

// decode video frame
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
avcodec_send_packet(pCodecCtx, &packet);
if (packet.size != 0)
avcodec_send_packet(pCodecCtx, &packet);
int len = avcodec_receive_frame(pCodecCtx, pFrame);
if (len == 0) {
len = pFrame->pkt_size;
Expand Down
29 changes: 23 additions & 6 deletions Core/HW/MediaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ MediaEngine::MediaEngine(): m_pdata(0) {
m_desHeight = 0;
m_decodingsize = 0;
m_bufSize = 0x2000;
m_videopts = 0;
m_pdata = 0;
m_demux = 0;
m_audioContext = 0;
Expand Down Expand Up @@ -171,7 +170,7 @@ void MediaEngine::closeMedia() {
}

void MediaEngine::DoState(PointerWrap &p) {
auto s = p.Section("MediaEngine", 1, 6);
auto s = p.Section("MediaEngine", 1, 7);
if (!s)
return;

Expand Down Expand Up @@ -213,6 +212,11 @@ void MediaEngine::DoState(PointerWrap &p) {
m_demux->DoState(p);

Do(p, m_videopts);
if (s >= 7) {
Do(p, m_lastPts);
} else {
m_lastPts = m_videopts;
}
Do(p, m_audiopts);

if (s >= 2) {
Expand Down Expand Up @@ -314,7 +318,7 @@ bool MediaEngine::openContext(bool keepReadPos) {

if (!SetupStreams()) {
// Fallback to old behavior. Reads too much and corrupts when game doesn't read fast enough.
// SetupStreams should work for newer FFmpeg 3.1+ now.
// SetupStreams sometimes work for newer FFmpeg 3.1+ now, but sometimes framerate is missing.
WARN_LOG_REPORT_ONCE(setupStreams, ME, "Failed to read valid video stream data from header");
if (avformat_find_stream_info(m_pFormatCtx, nullptr) < 0) {
closeContext();
Expand Down Expand Up @@ -390,6 +394,7 @@ bool MediaEngine::loadStream(const u8 *buffer, int readSize, int RingbufferSize)
closeMedia();

m_videopts = 0;
m_lastPts = -1;
m_audiopts = 0;
m_ringbuffersize = RingbufferSize;
m_pdata = new BufferQueue(RingbufferSize + 2048);
Expand Down Expand Up @@ -677,7 +682,8 @@ bool MediaEngine::stepVideo(int videoPixelMode, bool skipFrame) {
#endif

#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
avcodec_send_packet(m_pCodecCtx, &packet);
if (packet.size != 0)
avcodec_send_packet(m_pCodecCtx, &packet);
int result = avcodec_receive_frame(m_pCodecCtx, m_pFrame);
if (result == 0) {
result = m_pFrame->pkt_size;
Expand Down Expand Up @@ -712,10 +718,21 @@ bool MediaEngine::stepVideo(int videoPixelMode, bool skipFrame) {
int64_t bestPts = av_frame_get_best_effort_timestamp(m_pFrame);
int64_t ptsDuration = av_frame_get_pkt_duration(m_pFrame);
#endif
if (bestPts != AV_NOPTS_VALUE)
if (ptsDuration == 0) {
if (m_lastPts == bestPts - m_firstTimeStamp || bestPts == AV_NOPTS_VALUE) {
// TODO: Assuming 29.97 if missing.
m_videopts += 3003;
} else {
m_videopts = bestPts - m_firstTimeStamp;
m_lastPts = m_videopts;
}
} else if (bestPts != AV_NOPTS_VALUE) {
m_videopts = bestPts + ptsDuration - m_firstTimeStamp;
else
m_lastPts = m_videopts;
} else {
m_videopts += ptsDuration;
m_lastPts = m_videopts;
}
bGetFrame = true;
}
if (result <= 0 && dataEnd) {
Expand Down
3 changes: 2 additions & 1 deletion Core/HW/MediaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ class MediaEngine
int m_desHeight;
int m_decodingsize;
int m_bufSize;
s64 m_videopts;
s64 m_videopts = 0;
s64 m_lastPts = -1;
BufferQueue *m_pdata;

MpegDemux *m_demux;
Expand Down