From 68ded7cc592f53f7aa76427ab972e1c719aa80de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Damstedt=20Rasmussen?= Date: Sat, 2 Nov 2024 21:28:27 +0100 Subject: [PATCH] Fix video stream FPS extraction Use AVStream's avg_frame_rate for setting user presented FPS. --- .../TeamTalkLib/avstream/FFmpegStreamer.cpp | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/Library/TeamTalkLib/avstream/FFmpegStreamer.cpp b/Library/TeamTalkLib/avstream/FFmpegStreamer.cpp index f8e56e580..b70264575 100644 --- a/Library/TeamTalkLib/avstream/FFmpegStreamer.cpp +++ b/Library/TeamTalkLib/avstream/FFmpegStreamer.cpp @@ -187,6 +187,7 @@ AVFilterGraph* createVideoFilterGraph(AVFormatContext *fmt_ctx, void FillMediaFileProp(AVFormatContext *fmt_ctx, AVCodecContext *aud_dec_ctx, AVCodecContext *vid_dec_ctx, + int video_stream_index, MediaFileProp& out_prop) { if (aud_dec_ctx) @@ -194,28 +195,14 @@ void FillMediaFileProp(AVFormatContext *fmt_ctx, out_prop.audio = media::AudioFormat(aud_dec_ctx->sample_rate, aud_dec_ctx->channels); } - if(vid_dec_ctx) + if (vid_dec_ctx && video_stream_index >= 0) { // set frame rate - double fps = 1.0 / av_q2d(vid_dec_ctx->time_base) / std::max(vid_dec_ctx->ticks_per_frame, 1); - - /* it seems FFmpeg puts some bogus frame rate with - * 'vid_dec_ctx->time_base' set to 1/90000 when frame rate - * information is unavailable. This means that images embedded - * in mp3 files show up as video with a frame rate of 90000 - * fps. - * - * Note from libavcodec/decode.c: - * - * We do not currently have an API for passing the input timebase into decoders, - * but no filters used here should actually need it. - * So we make up some plausible-looking number (the MPEG 90kHz timebase) */ - if (int(fps) != 90000) - { - AVRational r_fps = av_d2q(fps, 1000); - out_prop.video = media::VideoFormat(vid_dec_ctx->width, vid_dec_ctx->height, - r_fps.num, r_fps.den, media::FOURCC_RGB32); - } + const AVStream* vidstream = fmt_ctx->streams[video_stream_index]; + out_prop.video = media::VideoFormat(vid_dec_ctx->width, vid_dec_ctx->height, + vidstream->avg_frame_rate.num, + vidstream->avg_frame_rate.den, + media::FOURCC_RGB32); } out_prop.duration_ms = (fmt_ctx->duration * av_q2d(AV_TIME_BASE_Q)) * 1000; @@ -233,7 +220,7 @@ bool GetAVMediaFileProp(const ACE_TString& filename, MediaFileProp& out_prop) audio_stream_index, video_stream_index)) return false; - FillMediaFileProp(fmt_ctx, aud_dec_ctx, vid_dec_ctx, out_prop); + FillMediaFileProp(fmt_ctx, aud_dec_ctx, vid_dec_ctx, video_stream_index, out_prop); out_prop.filename = filename; if (aud_dec_ctx) @@ -299,7 +286,7 @@ void FFmpegStreamer::Run() goto end; } - FillMediaFileProp(fmt_ctx, aud_dec_ctx, vid_dec_ctx, m_media_in); + FillMediaFileProp(fmt_ctx, aud_dec_ctx, vid_dec_ctx, video_stream_index, m_media_in); if (m_media_in.HasAudio() && !m_media_out.HasAudio() && m_media_out.audio_duration_ms) {