[FFmpeg-devel] [PATCH] Wrong duration in TS container (Ticket #1836)

Heesuk Jung heesuk.jung at lge.com
Sun Oct 21 07:21:32 CEST 2012


Fix indentation.

>From 2723ad65908531d136b828e4c9e615104a12beb8 Mon Sep 17 00:00:00 2001
From: Heesuk Jung <heesuk.jung at lge.com>
Date: Sun, 21 Oct 2012 13:21:23 +0900
Subject: [PATCH] Wrong duration in TS container (Ticket #1836)

Libavformat somtimes get wrong duration in some TS conatiner.
Please refer the problem description and file link in Ticket #1836.
(http://ffmpeg.org/trac/ffmpeg/ticket/1836)

I have just modified 2 point as below.

1. Duration determination considering time diff and file size / bit rate.

Duration is determined base on PTS time diff (end time - start ime) but
sometime is unreasonably so big.
I suggest that don't beleive duration based on PTS time diff
if time diff is bigger than hueristic value (file size / average bit rate *
2).

2. Change condition when estimate_timings_from_pts is called.

The estimate_timings_from_pts function is designed for MPEG-PS.
And so I changed condition from MPEG-TS to MPEG-PS
---
 libavformat/utils.c |   32 +++++++++++++++++++++++++++++---
 1 files changed, 29 insertions(+), 3 deletions(-)
 mode change 100644 => 100755 libavformat/utils.c

diff --git a/libavformat/utils.c b/libavformat/utils.c
old mode 100644
new mode 100755
index 745dcaa..d322907
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2035,6 +2035,29 @@ static int has_duration(AVFormatContext *ic)
 }
 
 /**
+ * examine if estimation from pts difference(end pts - start pts) is valid
or not.
+ * determine invalid duration if pts difference is un reasonably bigger
than file size/average bit rate.
+ * hueristically suggest  limit_filesize_multiple that means double file
size
+ * @return 1 if time_diff (end_time - start_time) is valid.
+ *         0 if time_diff (end_time - start_time) is not valid.
+ */
+static int valid_duration_from_pts(const int64_t start_time, const int64_t
end_time,
+                                    const int64_t filesize, int const
bit_rate)
+{
+    const uint8_t limit_filesize_multiple = 2;
+    const int64_t time_diff = end_time - start_time;
+
+    if (bit_rate != 0) {
+        if (time_diff >
(filesize*8/bit_rate*limit_filesize_multiple*1000000)) {
+            return 0;
+        }
+        return 1;
+    } else {
+        return 0;
+    }
+}
+
+/**
  * Estimate the stream timings from the one of each components.
  *
  * Also computes the global bitrate if possible.
@@ -2077,8 +2100,11 @@ static void update_stream_timings(AVFormatContext
*ic)
 
     if (start_time != INT64_MAX) {
         ic->start_time = start_time;
-        if (end_time != INT64_MIN)
-            duration = FFMAX(duration, end_time - start_time);
+        if (end_time != INT64_MIN) {
+            if (valid_duration_from_pts(start_time, end_time,
avio_size(ic->pb), ic->bit_rate)) {
+                duration = FFMAX(duration, end_time - start_time);
+            }
+        }
     }
     if (duration != INT64_MIN && duration > 0 && ic->duration ==
AV_NOPTS_VALUE) {
         ic->duration = duration;
@@ -2233,7 +2259,7 @@ static void estimate_timings(AVFormatContext *ic,
int64_t old_offset)
     }
 
     if ((!strcmp(ic->iformat->name, "mpeg") ||
-         !strcmp(ic->iformat->name, "mpegts")) &&
+         !strcmp(ic->iformat->name, "mpegps")) &&
         file_size && ic->pb->seekable) {
         /* get accurate estimate from the PTSes */
         estimate_timings_from_pts(ic, old_offset);
-- 
1.7.0.4

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Wrong-duration-in-TS-container-Ticket-1836.patch
Type: application/octet-stream
Size: 3360 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121021/a413e69c/attachment.obj>


More information about the ffmpeg-devel mailing list