[10/10] hls: Parse EXTINF duration as a floating-point number

Message ID 1375038091-92151-10-git-send-email-martin@martin.st
State Superseded
Headers show

Commit Message

Martin Storsjö July 28, 2013, 7:01 p.m.
From: Zhang Rui <bbcallen@gmail.com>

---
 libavformat/hls.c |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Luca Barbato July 28, 2013, 8:21 p.m. | #1
On 28/07/13 21:01, Martin Storsjö wrote:
> From: Zhang Rui <bbcallen@gmail.com>
> 
> ---
>  libavformat/hls.c |   11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 

Why?

lu
Martin Storsjö July 28, 2013, 8:26 p.m. | #2
On Sun, 28 Jul 2013, Luca Barbato wrote:

> On 28/07/13 21:01, Martin Storsjö wrote:
>> From: Zhang Rui <bbcallen@gmail.com>
>> 
>> ---
>>  libavformat/hls.c |   11 ++++++-----
>>  1 file changed, 6 insertions(+), 5 deletions(-)
>> 
>
> Why?

The durations are allowed to be non-integers (and they are that even in 
apple's own examples). If treating them as integers, e.g. duration sums 
will end up quite a bit off the real value.

// Martin

Patch

diff --git a/libavformat/hls.c b/libavformat/hls.c
index 7445cb8..b7070e4 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -56,7 +56,7 @@  enum KeyType {
 };
 
 struct segment {
-    int duration;
+    double duration;
     char url[MAX_URL_SIZE];
     char key[MAX_URL_SIZE];
     enum KeyType key_type;
@@ -202,7 +202,8 @@  static void handle_key_args(struct key_info *info, const char *key,
 static int parse_playlist(HLSContext *c, const char *url,
                           struct variant *var, AVIOContext *in)
 {
-    int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
+    int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
+    double duration = 0.0;
     enum KeyType key_type = KEY_NONE;
     uint8_t iv[16] = "";
     int has_iv = 0;
@@ -272,7 +273,7 @@  static int parse_playlist(HLSContext *c, const char *url,
                 var->finished = 1;
         } else if (av_strstart(line, "#EXTINF:", &ptr)) {
             is_segment = 1;
-            duration   = atoi(ptr);
+            duration   = atof(ptr);
         } else if (av_strstart(line, "#", NULL)) {
             continue;
         } else if (line[0]) {
@@ -480,8 +481,8 @@  static int hls_read_header(AVFormatContext *s)
     if (c->variants[0]->finished) {
         int64_t duration = 0;
         for (i = 0; i < c->variants[0]->n_segments; i++)
-            duration += c->variants[0]->segments[i]->duration;
-        s->duration = duration * AV_TIME_BASE;
+            duration += round(c->variants[0]->segments[i]->duration * AV_TIME_BASE);
+        s->duration = duration;
     }
 
     /* Open the demuxer for each variant */