mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-12-06 04:33:49 +01:00
Merge pull request #14176 from unknownbrackets/ffmpeg-update
Remove deprecated API calls for new FFmpeg 4.3.x
This commit is contained in:
5 files changed
+217
-84
No files matched your search
+96
-60
@@ -44,11 +44,12 @@ extern "C" {
|
||||
#define av_frame_free avcodec_free_frame
|
||||
#endif
|
||||
|
||||
static AVFormatContext* s_format_context = nullptr;
|
||||
static AVStream* s_stream = nullptr;
|
||||
static AVFrame* s_src_frame = nullptr;
|
||||
static AVFrame* s_scaled_frame = nullptr;
|
||||
static SwsContext* s_sws_context = nullptr;
|
||||
static AVFormatContext *s_format_context = nullptr;
|
||||
static AVCodecContext *s_codec_context = nullptr;
|
||||
static AVStream *s_stream = nullptr;
|
||||
static AVFrame *s_src_frame = nullptr;
|
||||
static AVFrame *s_scaled_frame = nullptr;
|
||||
static SwsContext *s_sws_context = nullptr;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -65,7 +66,9 @@ static void InitAVCodec() {
|
||||
static bool first_run = true;
|
||||
if (first_run) {
|
||||
#ifdef USE_FFMPEG
|
||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 12, 100)
|
||||
av_register_all();
|
||||
#endif
|
||||
#endif
|
||||
first_run = false;
|
||||
}
|
||||
@@ -87,51 +90,70 @@ bool AVIDump::Start(int w, int h)
|
||||
|
||||
bool AVIDump::CreateAVI() {
|
||||
#ifdef USE_FFMPEG
|
||||
AVCodec* codec = nullptr;
|
||||
AVCodec *codec = nullptr;
|
||||
|
||||
// Use gameID_EmulatedTimestamp for filename
|
||||
std::string discID = g_paramSFO.GetDiscID();
|
||||
std::string video_file_name = StringFromFormat("%s%s_%s.avi", GetSysDirectory(DIRECTORY_VIDEO).c_str(), discID.c_str(), KernelTimeNowFormatted().c_str()).c_str();
|
||||
|
||||
s_format_context = avformat_alloc_context();
|
||||
std::stringstream s_file_index_str;
|
||||
s_file_index_str << video_file_name;
|
||||
|
||||
snprintf(s_format_context->filename, sizeof(s_format_context->filename), "%s", s_file_index_str.str().c_str());
|
||||
INFO_LOG(COMMON, "Recording Video to: %s", s_format_context->filename);
|
||||
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 7, 0)
|
||||
char *filename = av_strdup(video_file_name.c_str());
|
||||
// Freed when the context is freed.
|
||||
s_format_context->url = filename;
|
||||
#else
|
||||
const char *filename = s_format_context->filename;
|
||||
snprintf(s_format_context->filename, sizeof(s_format_context->filename), "%s", video_file_name.c_str());
|
||||
#endif
|
||||
INFO_LOG(COMMON, "Recording Video to: %s", filename);
|
||||
|
||||
// Make sure that the path exists
|
||||
if (!File::Exists(GetSysDirectory(DIRECTORY_VIDEO)))
|
||||
File::CreateDir(GetSysDirectory(DIRECTORY_VIDEO));
|
||||
|
||||
if (File::Exists(s_format_context->filename))
|
||||
File::Delete(s_format_context->filename);
|
||||
if (File::Exists(filename))
|
||||
File::Delete(filename);
|
||||
|
||||
if (!(s_format_context->oformat = av_guess_format("avi", nullptr, nullptr)) || !(s_stream = avformat_new_stream(s_format_context, codec)))
|
||||
{
|
||||
s_format_context->oformat = av_guess_format("avi", nullptr, nullptr);
|
||||
if (!s_format_context->oformat)
|
||||
return false;
|
||||
s_stream = avformat_new_stream(s_format_context, codec);
|
||||
if (!s_stream)
|
||||
return false;
|
||||
}
|
||||
|
||||
s_stream->codec->codec_id = g_Config.bUseFFV1 ? AV_CODEC_ID_FFV1 : s_format_context->oformat->video_codec;
|
||||
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
|
||||
s_codec_context = s_stream->codec;
|
||||
#else
|
||||
s_codec_context = avcodec_alloc_context3(codec);
|
||||
#endif
|
||||
s_codec_context->codec_id = g_Config.bUseFFV1 ? AV_CODEC_ID_FFV1 : s_format_context->oformat->video_codec;
|
||||
if (!g_Config.bUseFFV1)
|
||||
s_stream->codec->codec_tag = MKTAG('X', 'V', 'I', 'D'); // Force XVID FourCC for better compatibility
|
||||
s_stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
||||
s_stream->codec->bit_rate = 400000;
|
||||
s_stream->codec->width = s_width;
|
||||
s_stream->codec->height = s_height;
|
||||
s_stream->codec->time_base.num = 1001;
|
||||
s_stream->codec->time_base.den = 60000;
|
||||
s_stream->codec->gop_size = 12;
|
||||
s_stream->codec->pix_fmt = g_Config.bUseFFV1 ? AV_PIX_FMT_BGRA : AV_PIX_FMT_YUV420P;
|
||||
s_codec_context->codec_tag = MKTAG('X', 'V', 'I', 'D'); // Force XVID FourCC for better compatibility
|
||||
s_codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
|
||||
s_codec_context->bit_rate = 400000;
|
||||
s_codec_context->width = s_width;
|
||||
s_codec_context->height = s_height;
|
||||
s_codec_context->time_base.num = 1001;
|
||||
s_codec_context->time_base.den = 60000;
|
||||
s_codec_context->gop_size = 12;
|
||||
s_codec_context->pix_fmt = g_Config.bUseFFV1 ? AV_PIX_FMT_BGRA : AV_PIX_FMT_YUV420P;
|
||||
|
||||
if (!(codec = avcodec_find_encoder(s_stream->codec->codec_id)) || (avcodec_open2(s_stream->codec, codec, nullptr) < 0))
|
||||
{
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
||||
if (avcodec_parameters_from_context(s_stream->codecpar, s_codec_context) < 0)
|
||||
return false;
|
||||
#endif
|
||||
|
||||
codec = avcodec_find_encoder(s_codec_context->codec_id);
|
||||
if (!codec)
|
||||
return false;
|
||||
if (avcodec_open2(s_codec_context, codec, nullptr) < 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
s_src_frame = av_frame_alloc();
|
||||
s_scaled_frame = av_frame_alloc();
|
||||
|
||||
s_scaled_frame->format = s_stream->codec->pix_fmt;
|
||||
s_scaled_frame->format = s_codec_context->pix_fmt;
|
||||
s_scaled_frame->width = s_width;
|
||||
s_scaled_frame->height = s_height;
|
||||
|
||||
@@ -139,14 +161,13 @@ bool AVIDump::CreateAVI() {
|
||||
if (av_frame_get_buffer(s_scaled_frame, 1))
|
||||
return false;
|
||||
#else
|
||||
if (avcodec_default_get_buffer(s_stream->codec, s_scaled_frame))
|
||||
if (avcodec_default_get_buffer(s_codec_context, s_scaled_frame))
|
||||
return false;
|
||||
#endif
|
||||
|
||||
NOTICE_LOG(G3D, "Opening file %s for dumping", s_format_context->filename);
|
||||
if (avio_open(&s_format_context->pb, s_format_context->filename, AVIO_FLAG_WRITE) < 0 || avformat_write_header(s_format_context, nullptr))
|
||||
{
|
||||
WARN_LOG(G3D, "Could not open %s", s_format_context->filename);
|
||||
NOTICE_LOG(G3D, "Opening file %s for dumping", filename);
|
||||
if (avio_open(&s_format_context->pb, filename, AVIO_FLAG_WRITE) < 0 || avformat_write_header(s_format_context, nullptr)) {
|
||||
WARN_LOG(G3D, "Could not open %s", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -166,8 +187,7 @@ static void PreparePacket(AVPacket* pkt) {
|
||||
|
||||
#endif
|
||||
|
||||
void AVIDump::AddFrame()
|
||||
{
|
||||
void AVIDump::AddFrame() {
|
||||
u32 w = 0;
|
||||
u32 h = 0;
|
||||
if (g_Config.bDumpVideoOutput) {
|
||||
@@ -192,44 +212,60 @@ void AVIDump::AddFrame()
|
||||
s_src_frame->height = s_height;
|
||||
|
||||
// Convert image from BGR24 to desired pixel format, and scale to initial width and height
|
||||
if ((s_sws_context = sws_getCachedContext(s_sws_context, w, h, AV_PIX_FMT_RGB24, s_width, s_height, s_stream->codec->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr)))
|
||||
{
|
||||
if ((s_sws_context = sws_getCachedContext(s_sws_context, w, h, AV_PIX_FMT_RGB24, s_width, s_height, s_codec_context->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr))) {
|
||||
sws_scale(s_sws_context, s_src_frame->data, s_src_frame->linesize, 0, h, s_scaled_frame->data, s_scaled_frame->linesize);
|
||||
}
|
||||
|
||||
s_scaled_frame->format = s_stream->codec->pix_fmt;
|
||||
s_scaled_frame->format = s_codec_context->pix_fmt;
|
||||
s_scaled_frame->width = s_width;
|
||||
s_scaled_frame->height = s_height;
|
||||
|
||||
// Encode and write the image.
|
||||
AVPacket pkt;
|
||||
PreparePacket(&pkt);
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
||||
int error = avcodec_send_frame(s_codec_context, s_scaled_frame);
|
||||
int got_packet = 0;
|
||||
if (avcodec_receive_packet(s_codec_context, &pkt) >= 0) {
|
||||
got_packet = 1;
|
||||
}
|
||||
#else
|
||||
int got_packet;
|
||||
int error = avcodec_encode_video2(s_stream->codec, &pkt, s_scaled_frame, &got_packet);
|
||||
while (!error && got_packet)
|
||||
{
|
||||
int error = avcodec_encode_video2(s_codec_context, &pkt, s_scaled_frame, &got_packet);
|
||||
#endif
|
||||
while (error >= 0 && got_packet) {
|
||||
// Write the compressed frame in the media file.
|
||||
if (pkt.pts != (s64)AV_NOPTS_VALUE)
|
||||
{
|
||||
pkt.pts = av_rescale_q(pkt.pts, s_stream->codec->time_base, s_stream->time_base);
|
||||
if (pkt.pts != (s64)AV_NOPTS_VALUE) {
|
||||
pkt.pts = av_rescale_q(pkt.pts, s_codec_context->time_base, s_stream->time_base);
|
||||
}
|
||||
if (pkt.dts != (s64)AV_NOPTS_VALUE)
|
||||
{
|
||||
pkt.dts = av_rescale_q(pkt.dts, s_stream->codec->time_base, s_stream->time_base);
|
||||
if (pkt.dts != (s64)AV_NOPTS_VALUE) {
|
||||
pkt.dts = av_rescale_q(pkt.dts, s_codec_context->time_base, s_stream->time_base);
|
||||
}
|
||||
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 60, 100)
|
||||
if (s_stream->codec->coded_frame->key_frame)
|
||||
if (s_codec_context->coded_frame->key_frame)
|
||||
pkt.flags |= AV_PKT_FLAG_KEY;
|
||||
#endif
|
||||
pkt.stream_index = s_stream->index;
|
||||
av_interleaved_write_frame(s_format_context, &pkt);
|
||||
|
||||
// Handle delayed frames.
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
||||
av_packet_unref(&pkt);
|
||||
error = avcodec_receive_packet(s_codec_context, &pkt);
|
||||
got_packet = error >= 0 ? 1 : 0;
|
||||
#else
|
||||
PreparePacket(&pkt);
|
||||
error = avcodec_encode_video2(s_stream->codec, &pkt, nullptr, &got_packet);
|
||||
error = avcodec_encode_video2(s_codec_context, &pkt, nullptr, &got_packet);
|
||||
#endif
|
||||
}
|
||||
if (error)
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
||||
av_packet_unref(&pkt);
|
||||
if (error < 0 && error != AVERROR(EAGAIN) && error != AVERROR_EOF)
|
||||
ERROR_LOG(G3D, "Error while encoding video: %d", error);
|
||||
#else
|
||||
if (error < 0)
|
||||
ERROR_LOG(G3D, "Error while encoding video: %d", error);
|
||||
#endif
|
||||
#endif
|
||||
delete[] flipbuffer;
|
||||
}
|
||||
@@ -246,18 +282,18 @@ void AVIDump::Stop() {
|
||||
|
||||
void AVIDump::CloseFile() {
|
||||
#ifdef USE_FFMPEG
|
||||
|
||||
if (s_stream)
|
||||
{
|
||||
if (s_stream->codec)
|
||||
{
|
||||
if (s_codec_context) {
|
||||
#if LIBAVCODEC_VERSION_MAJOR < 55
|
||||
avcodec_default_release_buffer(s_stream->codec, s_src_frame);
|
||||
avcodec_default_release_buffer(s_codec_context, s_src_frame);
|
||||
#endif
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
||||
avcodec_free_context(&s_codec_context);
|
||||
#else
|
||||
avcodec_close(s_codec_context);
|
||||
s_codec_context = nullptr;
|
||||
#endif
|
||||
avcodec_close(s_stream->codec);
|
||||
}
|
||||
av_freep(&s_stream);
|
||||
}
|
||||
av_freep(&s_stream);
|
||||
|
||||
av_frame_free(&s_src_frame);
|
||||
av_frame_free(&s_scaled_frame);
|
||||
|
||||
@@ -636,8 +636,12 @@ void __AtracInit() {
|
||||
atracIDTypes[5] = 0;
|
||||
|
||||
#ifdef USE_FFMPEG
|
||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 18, 100)
|
||||
avcodec_register_all();
|
||||
#endif
|
||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 12, 100)
|
||||
av_register_all();
|
||||
#endif
|
||||
#endif // USE_FFMPEG
|
||||
}
|
||||
|
||||
|
||||
+28
-3
@@ -393,9 +393,13 @@ void __MpegInit() {
|
||||
actionPostPut = __KernelRegisterActionType(PostPutAction::Create);
|
||||
|
||||
#ifdef USE_FFMPEG
|
||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 18, 100)
|
||||
avcodec_register_all();
|
||||
#endif
|
||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 12, 100)
|
||||
av_register_all();
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
void __MpegDoState(PointerWrap &p) {
|
||||
@@ -989,7 +993,21 @@ 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);
|
||||
int len = avcodec_receive_frame(pCodecCtx, pFrame);
|
||||
if (len == 0) {
|
||||
len = pFrame->pkt_size;
|
||||
got_picture = 1;
|
||||
} else if (len == AVERROR(EAGAIN)) {
|
||||
len = 0;
|
||||
got_picture = 0;
|
||||
} else {
|
||||
got_picture = 0;
|
||||
}
|
||||
#else
|
||||
int len = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);
|
||||
#endif
|
||||
DEBUG_LOG(ME, "got_picture %d", got_picture);
|
||||
if (got_picture){
|
||||
SwsContext *img_convert_ctx = NULL;
|
||||
@@ -1019,10 +1037,17 @@ static bool decodePmpVideo(PSPPointer<SceMpegRingBuffer> ringbuffer, u32 pmpctxA
|
||||
sws_freeContext(img_convert_ctx);
|
||||
|
||||
// update timestamp
|
||||
if (av_frame_get_best_effort_timestamp(mediaengine->m_pFrame) != AV_NOPTS_VALUE)
|
||||
mediaengine->m_videopts = av_frame_get_best_effort_timestamp(mediaengine->m_pFrame) + av_frame_get_pkt_duration(mediaengine->m_pFrame) - mediaengine->m_firstTimeStamp;
|
||||
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 58, 100)
|
||||
int64_t bestPts = mediaengine->m_pFrame->best_effort_timestamp;
|
||||
int64_t ptsDuration = mediaengine->m_pFrame->pkt_duration;
|
||||
#else
|
||||
int64_t bestPts = av_frame_get_best_effort_timestamp(mediaengine->m_pFrame);
|
||||
int64_t ptsDuration = av_frame_get_pkt_duration(mediaengine->m_pFrame);
|
||||
#endif
|
||||
if (bestPts != AV_NOPTS_VALUE)
|
||||
mediaengine->m_videopts = bestPts + ptsDuration - mediaengine->m_firstTimeStamp;
|
||||
else
|
||||
mediaengine->m_videopts += av_frame_get_pkt_duration(mediaengine->m_pFrame);
|
||||
mediaengine->m_videopts += ptsDuration;
|
||||
|
||||
// push the decoded frame into pmp_queue
|
||||
pmp_queue.push_back(pFrameRGB);
|
||||
|
||||
+67
-21
@@ -280,9 +280,8 @@ bool MediaEngine::SetupStreams() {
|
||||
for (int i = videoStreamNum + 1; i < m_expectedVideoStreams; i++) {
|
||||
addVideoStream(i);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -314,8 +313,10 @@ bool MediaEngine::openContext(bool keepReadPos) {
|
||||
av_dict_free(&open_opt);
|
||||
|
||||
if (!SetupStreams()) {
|
||||
// Fallback to old behavior.
|
||||
if (avformat_find_stream_info(m_pFormatCtx, NULL) < 0) {
|
||||
// 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.
|
||||
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();
|
||||
return false;
|
||||
}
|
||||
@@ -328,13 +329,19 @@ bool MediaEngine::openContext(bool keepReadPos) {
|
||||
|
||||
if (m_videoStream == -1) {
|
||||
// Find the first video stream
|
||||
for(int i = 0; i < (int)m_pFormatCtx->nb_streams; i++) {
|
||||
if(m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
|
||||
for (int i = 0; i < (int)m_pFormatCtx->nb_streams; i++) {
|
||||
const AVStream *s = m_pFormatCtx->streams[i];
|
||||
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
|
||||
AVMediaType type = s->codecpar->codec_type;
|
||||
#else
|
||||
AVMediaType type = s->codec->codec_type;
|
||||
#endif
|
||||
if (type == AVMEDIA_TYPE_VIDEO) {
|
||||
m_videoStream = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(m_videoStream == -1)
|
||||
if (m_videoStream == -1)
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -361,8 +368,13 @@ void MediaEngine::closeContext()
|
||||
av_free(m_pIOContext->buffer);
|
||||
if (m_pIOContext)
|
||||
av_free(m_pIOContext);
|
||||
for (auto it = m_pCodecCtxs.begin(), end = m_pCodecCtxs.end(); it != end; ++it)
|
||||
avcodec_close(it->second);
|
||||
for (auto it : m_pCodecCtxs) {
|
||||
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
|
||||
avcodec_free_context(&it.second);
|
||||
#else
|
||||
avcodec_close(it.second);
|
||||
#endif
|
||||
}
|
||||
m_pCodecCtxs.clear();
|
||||
if (m_pFormatCtx)
|
||||
avformat_close_input(&m_pFormatCtx);
|
||||
@@ -411,7 +423,12 @@ bool MediaEngine::addVideoStream(int streamNum, int streamId) {
|
||||
streamId = PSMF_VIDEO_STREAM_ID | streamNum;
|
||||
|
||||
stream->id = 0x00000100 | streamId;
|
||||
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
|
||||
stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
||||
stream->codecpar->codec_id = AV_CODEC_ID_H264;
|
||||
#else
|
||||
stream->request_probe = 0;
|
||||
#endif
|
||||
stream->need_parsing = AVSTREAM_PARSE_FULL;
|
||||
// We could set the width here, but we don't need to.
|
||||
if (streamNum >= m_expectedVideoStreams) {
|
||||
@@ -496,21 +513,29 @@ bool MediaEngine::setVideoStream(int streamNum, bool force) {
|
||||
if ((u32)streamNum >= m_pFormatCtx->nb_streams) {
|
||||
return false;
|
||||
}
|
||||
AVCodecContext *m_pCodecCtx = m_pFormatCtx->streams[streamNum]->codec;
|
||||
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57,33,100)
|
||||
AVCodecParameters *m_pCodecPar = m_pFormatCtx->streams[streamNum]->codecpar;
|
||||
|
||||
// Update from deprecated public codec context
|
||||
if (avcodec_parameters_from_context(m_pCodecPar, m_pCodecCtx) < 0) {
|
||||
AVStream *stream = m_pFormatCtx->streams[streamNum];
|
||||
int err;
|
||||
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
|
||||
AVCodec *pCodec = avcodec_find_decoder(stream->codecpar->codec_id);
|
||||
if (!pCodec) {
|
||||
WARN_LOG_REPORT(ME, "Could not find decoder for %d", (int)stream->codecpar->codec_id);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
AVCodecContext *m_pCodecCtx = avcodec_alloc_context3(pCodec);
|
||||
err = avcodec_parameters_to_context(m_pCodecCtx, stream->codecpar);
|
||||
if (err < 0) {
|
||||
WARN_LOG_REPORT(ME, "Failed to prepare context parameters: %08x", err);
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
AVCodecContext *m_pCodecCtx = stream->codec;
|
||||
// Find the decoder for the video stream
|
||||
AVCodec *pCodec = avcodec_find_decoder(m_pCodecCtx->codec_id);
|
||||
if (pCodec == nullptr) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
AVDictionary *opt = nullptr;
|
||||
// Allow ffmpeg to use any number of threads it wants. Without this, it doesn't use threads.
|
||||
@@ -640,9 +665,9 @@ bool MediaEngine::stepVideo(int videoPixelMode, bool skipFrame) {
|
||||
while (!bGetFrame) {
|
||||
bool dataEnd = av_read_frame(m_pFormatCtx, &packet) < 0;
|
||||
// Even if we've read all frames, some may have been re-ordered frames at the end.
|
||||
// Still need to decode those, so keep calling avcodec_decode_video2().
|
||||
// Still need to decode those, so keep calling avcodec_decode_video2() / avcodec_receive_frame().
|
||||
if (dataEnd || packet.stream_index == m_videoStream) {
|
||||
// avcodec_decode_video2() gives us the re-ordered frames with a NULL packet.
|
||||
// avcodec_decode_video2() / avcodec_send_packet() gives us the re-ordered frames with a NULL packet.
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
|
||||
if (dataEnd)
|
||||
av_packet_unref(&packet);
|
||||
@@ -651,7 +676,21 @@ bool MediaEngine::stepVideo(int videoPixelMode, bool skipFrame) {
|
||||
av_free_packet(&packet);
|
||||
#endif
|
||||
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
||||
avcodec_send_packet(m_pCodecCtx, &packet);
|
||||
int result = avcodec_receive_frame(m_pCodecCtx, m_pFrame);
|
||||
if (result == 0) {
|
||||
result = m_pFrame->pkt_size;
|
||||
frameFinished = 1;
|
||||
} else if (result == AVERROR(EAGAIN)) {
|
||||
result = 0;
|
||||
frameFinished = 0;
|
||||
} else {
|
||||
frameFinished = 0;
|
||||
}
|
||||
#else
|
||||
int result = avcodec_decode_video2(m_pCodecCtx, m_pFrame, &frameFinished, &packet);
|
||||
#endif
|
||||
if (frameFinished) {
|
||||
if (!m_pFrameRGB) {
|
||||
setVideoDim();
|
||||
@@ -666,10 +705,17 @@ bool MediaEngine::stepVideo(int videoPixelMode, bool skipFrame) {
|
||||
m_pCodecCtx->height, m_pFrameRGB->data, m_pFrameRGB->linesize);
|
||||
}
|
||||
|
||||
if (av_frame_get_best_effort_timestamp(m_pFrame) != AV_NOPTS_VALUE)
|
||||
m_videopts = av_frame_get_best_effort_timestamp(m_pFrame) + av_frame_get_pkt_duration(m_pFrame) - m_firstTimeStamp;
|
||||
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 58, 100)
|
||||
int64_t bestPts = m_pFrame->best_effort_timestamp;
|
||||
int64_t ptsDuration = m_pFrame->pkt_duration;
|
||||
#else
|
||||
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)
|
||||
m_videopts = bestPts + ptsDuration - m_firstTimeStamp;
|
||||
else
|
||||
m_videopts += av_frame_get_pkt_duration(m_pFrame);
|
||||
m_videopts += ptsDuration;
|
||||
bGetFrame = true;
|
||||
}
|
||||
if (result <= 0 && dataEnd) {
|
||||
|
||||
@@ -62,8 +62,12 @@ SimpleAudio::SimpleAudio(int audioType, int sample_rate, int channels)
|
||||
|
||||
void SimpleAudio::Init() {
|
||||
#ifdef USE_FFMPEG
|
||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 18, 100)
|
||||
avcodec_register_all();
|
||||
#endif
|
||||
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 12, 100)
|
||||
av_register_all();
|
||||
#endif
|
||||
InitFFmpeg();
|
||||
|
||||
frame_ = av_frame_alloc();
|
||||
@@ -188,7 +192,25 @@ bool SimpleAudio::Decode(void *inbuf, int inbytes, uint8_t *outbuf, int *outbyte
|
||||
|
||||
*outbytes = 0;
|
||||
srcPos = 0;
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
||||
if (inbytes != 0) {
|
||||
int err = avcodec_send_packet(codecCtx_, &packet);
|
||||
if (err < 0) {
|
||||
ERROR_LOG(ME, "Error sending audio frame to decoder (%d bytes): %d (%08x)", inbytes, err, err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int err = avcodec_receive_frame(codecCtx_, frame_);
|
||||
int len = 0;
|
||||
if (err >= 0) {
|
||||
len = frame_->pkt_size;
|
||||
got_frame = 1;
|
||||
} else if (err != AVERROR(EAGAIN)) {
|
||||
len = err;
|
||||
}
|
||||
#else
|
||||
int len = avcodec_decode_audio4(codecCtx_, frame_, &got_frame, &packet);
|
||||
#endif
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
|
||||
av_packet_unref(&packet);
|
||||
#else
|
||||
|
||||
Reference in new issue
Block a user