diff mbox

[FFmpeg-devel] avformat/utils: ignore outlier durations on subtitle/data streams as well

Message ID 20180419203520.46581-1-ffmpeg@tmm1.net
State Superseded
Headers show

Commit Message

Aman Karmani April 19, 2018, 8:35 p.m. UTC
From: Aman Gupta <aman@tmm1.net>

Similar to 4c9c4fe8b21, but for durations. This fixes #7151, where
the report duration and bitrate on a mpegts stream is wildly off
due to the dvb_teletext stream's timings.
---
 libavformat/utils.c | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)

Comments

Michael Niedermayer April 19, 2018, 11:43 p.m. UTC | #1
On Thu, Apr 19, 2018 at 01:35:20PM -0700, Aman Gupta wrote:
> From: Aman Gupta <aman@tmm1.net>
> 
> Similar to 4c9c4fe8b21, but for durations. This fixes #7151, where
> the report duration and bitrate on a mpegts stream is wildly off
> due to the dvb_teletext stream's timings.
> ---
>  libavformat/utils.c | 33 +++++++++++++++++++++------------
>  1 file changed, 21 insertions(+), 12 deletions(-)
> 
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index f2f2cc4239..c685d260f7 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -2618,9 +2618,8 @@ static int has_duration(AVFormatContext *ic)
>  static void update_stream_timings(AVFormatContext *ic)
>  {
>      int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
> -    int64_t duration, duration1, filesize;
> +    int64_t duration, duration1, duration_text, filesize;
>      int i;
> -    AVStream *st;
>      AVProgram *p;
>  
>      start_time = INT64_MAX;
> @@ -2628,22 +2627,25 @@ static void update_stream_timings(AVFormatContext *ic)
>      end_time   = INT64_MIN;
>      end_time_text   = INT64_MIN;
>      duration   = INT64_MIN;
> +    duration_text = INT64_MIN;
> +
>      for (i = 0; i < ic->nb_streams; i++) {
> -        st = ic->streams[i];
> +        AVStream *st = ic->streams[i];
> +        int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
> +                      st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
>          if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
>              start_time1 = av_rescale_q(st->start_time, st->time_base,
>                                         AV_TIME_BASE_Q);
> -            if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
> -                if (start_time1 < start_time_text)
> -                    start_time_text = start_time1;
> -            } else
> +            if (is_text)
> +                start_time_text = FFMIN(start_time_text, start_time1);
> +            else
>                  start_time = FFMIN(start_time, start_time1);
>              end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
>                                           AV_TIME_BASE_Q,
>                                           AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
>              if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
>                  end_time1 += start_time1;

> -                if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
> +                if (is_text)

please split the code factorization into a seperate patch

thx

[...]
diff mbox

Patch

diff --git a/libavformat/utils.c b/libavformat/utils.c
index f2f2cc4239..c685d260f7 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2618,9 +2618,8 @@  static int has_duration(AVFormatContext *ic)
 static void update_stream_timings(AVFormatContext *ic)
 {
     int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
-    int64_t duration, duration1, filesize;
+    int64_t duration, duration1, duration_text, filesize;
     int i;
-    AVStream *st;
     AVProgram *p;
 
     start_time = INT64_MAX;
@@ -2628,22 +2627,25 @@  static void update_stream_timings(AVFormatContext *ic)
     end_time   = INT64_MIN;
     end_time_text   = INT64_MIN;
     duration   = INT64_MIN;
+    duration_text = INT64_MIN;
+
     for (i = 0; i < ic->nb_streams; i++) {
-        st = ic->streams[i];
+        AVStream *st = ic->streams[i];
+        int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
+                      st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
         if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
             start_time1 = av_rescale_q(st->start_time, st->time_base,
                                        AV_TIME_BASE_Q);
-            if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
-                if (start_time1 < start_time_text)
-                    start_time_text = start_time1;
-            } else
+            if (is_text)
+                start_time_text = FFMIN(start_time_text, start_time1);
+            else
                 start_time = FFMIN(start_time, start_time1);
             end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
                                          AV_TIME_BASE_Q,
                                          AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
             if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
                 end_time1 += start_time1;
-                if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
+                if (is_text)
                     end_time_text = FFMAX(end_time_text, end_time1);
                 else
                     end_time = FFMAX(end_time, end_time1);
@@ -2658,7 +2660,10 @@  static void update_stream_timings(AVFormatContext *ic)
         if (st->duration != AV_NOPTS_VALUE) {
             duration1 = av_rescale_q(st->duration, st->time_base,
                                      AV_TIME_BASE_Q);
-            duration  = FFMAX(duration, duration1);
+             if (is_text)
+                 duration_text = FFMAX(duration_text, duration1);
+             else
+                 duration = FFMAX(duration, duration1);
         }
     }
     if (start_time == INT64_MAX || (start_time > start_time_text && start_time - start_time_text < AV_TIME_BASE))
@@ -2666,11 +2671,15 @@  static void update_stream_timings(AVFormatContext *ic)
     else if (start_time > start_time_text)
         av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
 
-    if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE)) {
+    if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
         end_time = end_time_text;
-    } else if (end_time < end_time_text) {
+    else if (end_time < end_time_text)
         av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
-    }
+
+     if (duration == INT64_MIN || (duration < duration_text && duration_text - duration < AV_TIME_BASE))
+         duration = duration_text;
+     else if (duration < duration_text)
+         av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
 
     if (start_time != INT64_MAX) {
         ic->start_time = start_time;