Skip to content

Commit

Permalink
Fix video stream FPS extraction
Browse files Browse the repository at this point in the history
Use AVStream's avg_frame_rate for setting user presented FPS.
  • Loading branch information
bear101 committed Nov 2, 2024
1 parent c59210e commit 68ded7c
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions Library/TeamTalkLib/avstream/FFmpegStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,35 +187,22 @@ 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)
{
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;
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 68ded7c

Please sign in to comment.