diff mbox series

[FFmpeg-devel,2/2] avformat/flvdec: make key frame timestamps more accurate

Message ID tencent_A74E9D701F2DAA9A6640579A149F67AB1808@qq.com
State New
Headers show
Series [FFmpeg-devel,1/2] avformat/flvenc: fix timestamp of key frame index | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

zhilizhao(赵志立) July 14, 2022, 4:48 a.m. UTC
From: Zhao Zhili <zhilizhao@tencent.com>

There is an implicit cast from double to int64_t in time unit of
second.
---
 libavformat/flvdec.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

Comments

Michael Niedermayer July 16, 2022, 1:53 p.m. UTC | #1
On Thu, Jul 14, 2022 at 12:48:14PM +0800, Zhao Zhili wrote:
> From: Zhao Zhili <zhilizhao@tencent.com>
> 
> There is an implicit cast from double to int64_t in time unit of
> second.
> ---
>  libavformat/flvdec.c | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)
> 
> diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
> index c5d3c63bd0..a78d720295 100644
> --- a/libavformat/flvdec.c
> +++ b/libavformat/flvdec.c
> @@ -146,9 +146,9 @@ static void add_keyframes_index(AVFormatContext *s)
>      if (ffstream(stream)->nb_index_entries == 0) {
>          for (i = 0; i < flv->keyframe_count; i++) {
>              av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
> -                   flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
> +                   flv->keyframe_filepositions[i], flv->keyframe_times[i]);
>              av_add_index_entry(stream, flv->keyframe_filepositions[i],
> -                flv->keyframe_times[i] * 1000, 0, 0, AVINDEX_KEYFRAME);
> +                flv->keyframe_times[i], 0, 0, AVINDEX_KEYFRAME);
>          }
>      } else
>          av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
> @@ -461,9 +461,13 @@ static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t m
>              d = av_int2double(avio_rb64(ioc));
>              if (isnan(d) || d < INT64_MIN || d > INT64_MAX)
>                  goto invalid;
> -            if (current_array == &times && (d <= INT64_MIN / 1000 || d >= INT64_MAX / 1000))
> -                goto invalid;
> -            current_array[0][i] = d;
> +            if (current_array == &times) {
> +                if (d <= INT64_MIN / 1000 || d >= INT64_MAX / 1000)
> +                    goto invalid;
> +                current_array[0][i] = d * 1000;
> +            } else {
> +                current_array[0][i] = d;
> +            }
>          }
>          if (times && filepositions) {
>              // All done, exiting at a position allowing amf_parse_object
> @@ -476,7 +480,7 @@ static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t m
>      if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
>          for (i = 0; i < FFMIN(2,fileposlen); i++) {
>              flv->validate_index[i].pos = filepositions[i];
> -            flv->validate_index[i].dts = times[i] * 1000;
> +            flv->validate_index[i].dts = times[i];
>              flv->validate_count        = i + 1;
>          }
>          flv->keyframe_times = times;

can be simplified like this:


diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index a78d720295..3155b2a517 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -428,6 +428,7 @@ static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t m
            amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
         int64_t **current_array;
         unsigned int arraylen;
+        int factor;
 
         // Expect array object in context
         if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
@@ -440,10 +441,12 @@ static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t m
         if       (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
             current_array = &times;
             timeslen      = arraylen;
+            factor        = 1000;
         } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
                    !filepositions) {
             current_array = &filepositions;
             fileposlen    = arraylen;
+            factor        = 1;
         } else
             // unexpected metatag inside keyframes, will not use such
             // metadata for indexing
@@ -458,16 +461,10 @@ static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t m
             double d;
             if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
                 goto invalid;
-            d = av_int2double(avio_rb64(ioc));
+            d = av_int2double(avio_rb64(ioc)) * factor;
             if (isnan(d) || d < INT64_MIN || d > INT64_MAX)
                 goto invalid;
-            if (current_array == &times) {
-                if (d <= INT64_MIN / 1000 || d >= INT64_MAX / 1000)
-                    goto invalid;
-                current_array[0][i] = d * 1000;
-            } else {
-                current_array[0][i] = d;
-            }
+            current_array[0][i] = d;
         }
         if (times && filepositions) {
             // All done, exiting at a position allowing amf_parse_object
[...]
diff mbox series

Patch

diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index c5d3c63bd0..a78d720295 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -146,9 +146,9 @@  static void add_keyframes_index(AVFormatContext *s)
     if (ffstream(stream)->nb_index_entries == 0) {
         for (i = 0; i < flv->keyframe_count; i++) {
             av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
-                   flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
+                   flv->keyframe_filepositions[i], flv->keyframe_times[i]);
             av_add_index_entry(stream, flv->keyframe_filepositions[i],
-                flv->keyframe_times[i] * 1000, 0, 0, AVINDEX_KEYFRAME);
+                flv->keyframe_times[i], 0, 0, AVINDEX_KEYFRAME);
         }
     } else
         av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
@@ -461,9 +461,13 @@  static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t m
             d = av_int2double(avio_rb64(ioc));
             if (isnan(d) || d < INT64_MIN || d > INT64_MAX)
                 goto invalid;
-            if (current_array == &times && (d <= INT64_MIN / 1000 || d >= INT64_MAX / 1000))
-                goto invalid;
-            current_array[0][i] = d;
+            if (current_array == &times) {
+                if (d <= INT64_MIN / 1000 || d >= INT64_MAX / 1000)
+                    goto invalid;
+                current_array[0][i] = d * 1000;
+            } else {
+                current_array[0][i] = d;
+            }
         }
         if (times && filepositions) {
             // All done, exiting at a position allowing amf_parse_object
@@ -476,7 +480,7 @@  static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t m
     if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
         for (i = 0; i < FFMIN(2,fileposlen); i++) {
             flv->validate_index[i].pos = filepositions[i];
-            flv->validate_index[i].dts = times[i] * 1000;
+            flv->validate_index[i].dts = times[i];
             flv->validate_count        = i + 1;
         }
         flv->keyframe_times = times;