diff mbox

[FFmpeg-devel] lavf: consider codec framerate for framerate detection

Message ID 20170531162601.21287-1-nfxjfg@googlemail.com
State Accepted
Commit 66cf78e932956eaa7d5a9dab8766efbfc1431e55
Headers show

Commit Message

wm4 May 31, 2017, 4:26 p.m. UTC
Fixes detection of some TV sample as 24.5 FPS. With the patch applied,
it's detected as 25 FPS.

This is enabled for mpegts only.
---
 libavformat/internal.h |  5 +++++
 libavformat/mpegts.c   |  2 ++
 libavformat/utils.c    | 10 ++++++++++
 3 files changed, 17 insertions(+)

Comments

wm4 June 7, 2017, 10:33 a.m. UTC | #1
On Wed, 31 May 2017 18:26:01 +0200
wm4 <nfxjfg@googlemail.com> wrote:

> Fixes detection of some TV sample as 24.5 FPS. With the patch applied,
> it's detected as 25 FPS.
> 
> This is enabled for mpegts only.
> ---
>  libavformat/internal.h |  5 +++++
>  libavformat/mpegts.c   |  2 ++
>  libavformat/utils.c    | 10 ++++++++++
>  3 files changed, 17 insertions(+)
> 
> diff --git a/libavformat/internal.h b/libavformat/internal.h
> index c856945ce9..d136c79bdd 100644
> --- a/libavformat/internal.h
> +++ b/libavformat/internal.h
> @@ -145,6 +145,11 @@ struct AVFormatInternal {
>       * ID3v2 tag useful for MP3 demuxing
>       */
>      AVDictionary *id3v2_meta;
> +
> +    /*
> +     * Prefer the codec framerate for avg_frame_rate computation.
> +     */
> +    int prefer_codec_framerate;
>  };
>  
>  struct AVStreamInternal {
> diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
> index 3eff1522bd..4d2f5c6802 100644
> --- a/libavformat/mpegts.c
> +++ b/libavformat/mpegts.c
> @@ -2616,6 +2616,8 @@ static int mpegts_read_header(AVFormatContext *s)
>      int len;
>      int64_t pos, probesize = s->probesize;
>  
> +    s->internal->prefer_codec_framerate = 1;
> +
>      if (ffio_ensure_seekback(pb, probesize) < 0)
>          av_log(s, AV_LOG_WARNING, "Failed to allocate buffers for seekback\n");
>  
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index fbd8b58ac2..b50ca2f7ac 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -3903,6 +3903,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
>                  st->info->codec_info_duration) {
>                  int best_fps      = 0;
>                  double best_error = 0.01;
> +                AVRational codec_frame_rate = avctx->framerate;
>  
>                  if (st->info->codec_info_duration        >= INT64_MAX / st->time_base.num / 2||
>                      st->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
> @@ -3923,6 +3924,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
>                          best_error = error;
>                          best_fps   = std_fps.num;
>                      }
> +
> +                    if (ic->internal->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
> +                        error       = fabs(av_q2d(codec_frame_rate) /
> +                                           av_q2d(std_fps) - 1);
> +                        if (error < best_error) {
> +                            best_error = error;
> +                            best_fps   = std_fps.num;
> +                        }
> +                    }
>                  }
>                  if (best_fps)
>                      av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,

Pushed.
Marton Balint June 7, 2017, 7:23 p.m. UTC | #2
On Wed, 7 Jun 2017, wm4 wrote:

> On Wed, 31 May 2017 18:26:01 +0200
> wm4 <nfxjfg@googlemail.com> wrote:
>
>> Fixes detection of some TV sample as 24.5 FPS. With the patch applied,
>> it's detected as 25 FPS.
>> 
>> This is enabled for mpegts only.
>> ---
>>  libavformat/internal.h |  5 +++++
>>  libavformat/mpegts.c   |  2 ++
>>  libavformat/utils.c    | 10 ++++++++++
>>  3 files changed, 17 insertions(+)
>> 
>> diff --git a/libavformat/internal.h b/libavformat/internal.h
>> index c856945ce9..d136c79bdd 100644
>> --- a/libavformat/internal.h
>> +++ b/libavformat/internal.h
>> @@ -145,6 +145,11 @@ struct AVFormatInternal {
>>       * ID3v2 tag useful for MP3 demuxing
>>       */
>>      AVDictionary *id3v2_meta;
>> +
>> +    /*
>> +     * Prefer the codec framerate for avg_frame_rate computation.
>> +     */
>> +    int prefer_codec_framerate;

Does this approach have any benefit over signalling this feature as a 
demuxer flag?

Thanks,
Marton
wm4 June 8, 2017, 8:46 a.m. UTC | #3
On Wed, 7 Jun 2017 21:23:12 +0200 (CEST)
Marton Balint <cus@passwd.hu> wrote:

> On Wed, 7 Jun 2017, wm4 wrote:
> 
> > On Wed, 31 May 2017 18:26:01 +0200
> > wm4 <nfxjfg@googlemail.com> wrote:
> >  
> >> Fixes detection of some TV sample as 24.5 FPS. With the patch applied,
> >> it's detected as 25 FPS.
> >> 
> >> This is enabled for mpegts only.
> >> ---
> >>  libavformat/internal.h |  5 +++++
> >>  libavformat/mpegts.c   |  2 ++
> >>  libavformat/utils.c    | 10 ++++++++++
> >>  3 files changed, 17 insertions(+)
> >> 
> >> diff --git a/libavformat/internal.h b/libavformat/internal.h
> >> index c856945ce9..d136c79bdd 100644
> >> --- a/libavformat/internal.h
> >> +++ b/libavformat/internal.h
> >> @@ -145,6 +145,11 @@ struct AVFormatInternal {
> >>       * ID3v2 tag useful for MP3 demuxing
> >>       */
> >>      AVDictionary *id3v2_meta;
> >> +
> >> +    /*
> >> +     * Prefer the codec framerate for avg_frame_rate computation.
> >> +     */
> >> +    int prefer_codec_framerate;  
> 
> Does this approach have any benefit over signalling this feature as a 
> demuxer flag?

There are no private demuxer flags AFAIK, but there are other flags in
the internal struct. So seemed like a good choice.
diff mbox

Patch

diff --git a/libavformat/internal.h b/libavformat/internal.h
index c856945ce9..d136c79bdd 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -145,6 +145,11 @@  struct AVFormatInternal {
      * ID3v2 tag useful for MP3 demuxing
      */
     AVDictionary *id3v2_meta;
+
+    /*
+     * Prefer the codec framerate for avg_frame_rate computation.
+     */
+    int prefer_codec_framerate;
 };
 
 struct AVStreamInternal {
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 3eff1522bd..4d2f5c6802 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -2616,6 +2616,8 @@  static int mpegts_read_header(AVFormatContext *s)
     int len;
     int64_t pos, probesize = s->probesize;
 
+    s->internal->prefer_codec_framerate = 1;
+
     if (ffio_ensure_seekback(pb, probesize) < 0)
         av_log(s, AV_LOG_WARNING, "Failed to allocate buffers for seekback\n");
 
diff --git a/libavformat/utils.c b/libavformat/utils.c
index fbd8b58ac2..b50ca2f7ac 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -3903,6 +3903,7 @@  FF_ENABLE_DEPRECATION_WARNINGS
                 st->info->codec_info_duration) {
                 int best_fps      = 0;
                 double best_error = 0.01;
+                AVRational codec_frame_rate = avctx->framerate;
 
                 if (st->info->codec_info_duration        >= INT64_MAX / st->time_base.num / 2||
                     st->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
@@ -3923,6 +3924,15 @@  FF_ENABLE_DEPRECATION_WARNINGS
                         best_error = error;
                         best_fps   = std_fps.num;
                     }
+
+                    if (ic->internal->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
+                        error       = fabs(av_q2d(codec_frame_rate) /
+                                           av_q2d(std_fps) - 1);
+                        if (error < best_error) {
+                            best_error = error;
+                            best_fps   = std_fps.num;
+                        }
+                    }
                 }
                 if (best_fps)
                     av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,