diff mbox series

[FFmpeg-devel] avformat/hlsenc: compute video_keyframe_size after write keyframe

Message ID 20200918015327.63915-1-lq@chinaffmpeg.org
State Accepted
Commit b5ca8f2c66954614d81579082025f580efc0cffc
Headers show
Series [FFmpeg-devel] avformat/hlsenc: compute video_keyframe_size after write keyframe
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Steven Liu Sept. 18, 2020, 1:53 a.m. UTC
fix ticket: 8636
When write keyframe and the keyframe is the frist packet of the segment,
then compute the size of the keyframe which have been write into segment
first packet. and set the start position of the segment, should not use
avio_tell(vs->out) to get the keyframe position, because it can be set
to 0 if close at above of the workflow, that maybe inaccurate, but the
start_pos can be used here, because start_pos is set after write
the previous packet.

Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
---
 libavformat/hlsenc.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Steven Liu Oct. 20, 2020, 3:43 a.m. UTC | #1
Steven Liu <lq@chinaffmpeg.org> 于2020年9月18日周五 上午9:53写道:
>
> fix ticket: 8636
> When write keyframe and the keyframe is the frist packet of the segment,
> then compute the size of the keyframe which have been write into segment
> first packet. and set the start position of the segment, should not use
> avio_tell(vs->out) to get the keyframe position, because it can be set
> to 0 if close at above of the workflow, that maybe inaccurate, but the
> start_pos can be used here, because start_pos is set after write
> the previous packet.
>
> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
> ---
>  libavformat/hlsenc.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
>
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index cb31d6aed7..8687d7c12c 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -2572,13 +2572,14 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
>
>      vs->packets_written++;
>      if (oc->pb) {
> +        int64_t keyframe_pre_pos = avio_tell(oc->pb);
>          ret = ff_write_chained(oc, stream_index, pkt, s, 0);
> -        vs->video_keyframe_size += pkt->size;
> -        if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) && (pkt->flags & AV_PKT_FLAG_KEY)) {
> -            vs->video_keyframe_size = avio_tell(oc->pb);
> -        } else {
> -            vs->video_keyframe_pos = avio_tell(vs->out);
> +        if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
> +            (pkt->flags & AV_PKT_FLAG_KEY) && !keyframe_pre_pos) {
> +            av_write_frame(oc, NULL); /* Flush any buffered data */
> +            vs->video_keyframe_size = avio_tell(oc->pb) - keyframe_pre_pos;
>          }
> +        vs->video_keyframe_pos = vs->start_pos;
>          if (hls->ignore_io_errors)
>              ret = 0;
>      }
> --
> 2.25.0
>
>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

Pushed


Thanks
Steven
diff mbox series

Patch

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index cb31d6aed7..8687d7c12c 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -2572,13 +2572,14 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
 
     vs->packets_written++;
     if (oc->pb) {
+        int64_t keyframe_pre_pos = avio_tell(oc->pb);
         ret = ff_write_chained(oc, stream_index, pkt, s, 0);
-        vs->video_keyframe_size += pkt->size;
-        if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) && (pkt->flags & AV_PKT_FLAG_KEY)) {
-            vs->video_keyframe_size = avio_tell(oc->pb);
-        } else {
-            vs->video_keyframe_pos = avio_tell(vs->out);
+        if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
+            (pkt->flags & AV_PKT_FLAG_KEY) && !keyframe_pre_pos) {
+            av_write_frame(oc, NULL); /* Flush any buffered data */
+            vs->video_keyframe_size = avio_tell(oc->pb) - keyframe_pre_pos;
         }
+        vs->video_keyframe_pos = vs->start_pos;
         if (hls->ignore_io_errors)
             ret = 0;
     }