[PATCH] avformat/hls Implement support for using AVSEEK_FLAG_BACKWARD when seeking
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #6850. --- libavformat/hls.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..3f1fd2cb70 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2199,10 +2199,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, - tb.den, AV_ROUND_DOWN) - - pls->seek_timestamp; - if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || - pls->pkt->flags & AV_PKT_FLAG_KEY)) { + tb.den, AV_ROUND_DOWN) - + pls->seek_timestamp; + /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if ((pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) || + (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || + pls->pkt->flags & AV_PKT_FLAG_KEY))) { pls->seek_timestamp = AV_NOPTS_VALUE; break; } -- 2.25.1
Den fre 14 maj 2021 21:42Gustav Grusell <gustav.grusell@gmail.com> skrev:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #6850. --- libavformat/hls.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..3f1fd2cb70 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2199,10 +2199,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, - tb.den, AV_ROUND_DOWN) - - pls->seek_timestamp; - if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || - pls->pkt->flags & AV_PKT_FLAG_KEY)) { + tb.den, AV_ROUND_DOWN) - + pls->seek_timestamp; + /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if ((pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) || + (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || + pls->pkt->flags & AV_PKT_FLAG_KEY))) { pls->seek_timestamp = AV_NOPTS_VALUE; break; }
Did anyone find time to look at this? Any comments? Cheers, Gustav
On Fri, 14 May 2021 21:42:25 +0200 Gustav Grusell <gustav.grusell@gmail.com> wrote:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #6850. --- libavformat/hls.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..3f1fd2cb70 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2199,10 +2199,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, - tb.den, AV_ROUND_DOWN) - - pls->seek_timestamp; - if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || - pls->pkt->flags & AV_PKT_FLAG_KEY)) { + tb.den, AV_ROUND_DOWN) - + pls->seek_timestamp; only format change, maybe better to revert if you don't mean to do so :)
+ /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if ((pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) || + (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || + pls->pkt->flags & AV_PKT_FLAG_KEY))) { pls->seek_timestamp = AV_NOPTS_VALUE; break; }
Regards, Lingjiang Fang
On Sat, May 15, 2021 at 4:11 AM Gustav Grusell <gustav.grusell@gmail.com> wrote:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #6850. --- libavformat/hls.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..3f1fd2cb70 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2199,10 +2199,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, - tb.den, AV_ROUND_DOWN) - - pls->seek_timestamp; - if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || - pls->pkt->flags & AV_PKT_FLAG_KEY)) { + tb.den, AV_ROUND_DOWN) - + pls->seek_timestamp; + /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if ((pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) || + (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || + pls->pkt->flags & AV_PKT_FLAG_KEY))) { pls->seek_timestamp = AV_NOPTS_VALUE; break; }
I think you can put your code above this line ( tb = get_timebase(pls). ) to avoid invalid code execution and keep the code aligned Regards, bevis
On Tue, May 25, 2021 at 4:24 AM Lingjiang Fang <vacingfang@foxmail.com> wrote:
only format change, maybe better to revert if you don't mean to do so :)
Thanks, will fix. On Tue, May 25, 2021 at 10:30 AM 徐慧书 <javashu2012@gmail.com> wrote:
On Sat, May 15, 2021 at 4:11 AM Gustav Grusell <gustav.grusell@gmail.com> wrote:
tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts,
AV_TIME_BASE,
- tb.den, AV_ROUND_DOWN) - - pls->seek_timestamp; - if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || - pls->pkt->flags & AV_PKT_FLAG_KEY)) { + tb.den, AV_ROUND_DOWN) - + pls->seek_timestamp; + /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if ((pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) || + (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || + pls->pkt->flags & AV_PKT_FLAG_KEY))) { pls->seek_timestamp = AV_NOPTS_VALUE; break; }
I think you can put your code above this line ( tb = get_timebase(pls). ) to avoid invalid code execution and keep the code aligned
Good suggestion, I will do so and provide a new patch.
Regards, Gustav
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485. Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..fb8c9b071a 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2197,6 +2197,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) break; } + /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if (pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) { + pls->seek_timestamp = AV_NOPTS_VALUE; + break; + } + tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, tb.den, AV_ROUND_DOWN) - -- 2.25.1
On Tue, May 25, 2021 at 2:52 PM Gustav Grusell <gustav.grusell@gmail.com> wrote:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485.
Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 9 +++++++++ 1 file changed, 9 insertions(+)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..fb8c9b071a 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2197,6 +2197,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) break; }
+ /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if (pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) { + pls->seek_timestamp = AV_NOPTS_VALUE; + break; + } + tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, tb.den, AV_ROUND_DOWN) - -- 2.25.1
Are there any further comments on this? If there is anything I can do to increase the likelihood of getting this accepted please let me know. Cheers, Gustav
You forgot to mention #6850. Also you patch was not seen by patchwork. Please use "git send-email -v2 -1" to send v2 patch. Thanks.
Den lör 5 juni 2021 10:14Valerii Zapodovnikov <val.zapod.vz@gmail.com> skrev:
You forgot to mention #6850. Also you patch was not seen by patchwork. Please use "git send-email -v2 -1" to send v2 patch. Thanks.
Thanks, I will take care of this.
BTW, what about #7359?
On Sat, Jun 5, 2021 at 6:30 PM Valerii Zapodovnikov <val.zapod.vz@gmail.com> wrote:
BTW, what about #7359?
#7359 is not fixed by my patch, but I thought I would look into that after I get this patch done. I tried the old patch for #7359 but as far as I could tell it didn't completely solve the problem.
Regarding #6850, I realized my patch doesn't exactly fix it. In the scenario described in the bug report, ffplay will use byte seek, so code touched by my patch is not used then. I haven't yet been able to figure out why the byte seek operation does not work as expected. However, my patch will allow correct seek to the beginning of the file if ffplay is run with byte seek disabled (-bytes 0 on the commandline)
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485. Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..fb8c9b071a 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2197,6 +2197,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) break; } + /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if (pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) { + pls->seek_timestamp = AV_NOPTS_VALUE; + break; + } + tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, tb.den, AV_ROUND_DOWN) - -- 2.25.1
On Sat, Jun 5, 2021 at 10:41 PM Gustav Grusell <gustav.grusell@gmail.com> wrote:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485.
Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 9 +++++++++ 1 file changed, 9 insertions(+)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..fb8c9b071a 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2197,6 +2197,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) break; }
+ /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if (pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) { + pls->seek_timestamp = AV_NOPTS_VALUE; + break; + } + tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, tb.den, AV_ROUND_DOWN) - -- 2.25.1
Any further comments on this? Btw, it seems the v2 patch got picked up as a separate patch by patchwork. Is this how it is supposed to work or did I miss something when doing git send-email ? cheers, Gustav
On Sat, 5 Jun 2021 22:42:26 +0200 Gustav Grusell <gustav.grusell@gmail.com> wrote:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485.
Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 9 +++++++++ 1 file changed, 9 insertions(+)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..fb8c9b071a 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2197,6 +2197,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) break; }
+ /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if (pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) { taking account of the case of using flag backward and flag any together, a logic like this is better:
if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD && (pls->seek_flags & AVSEEK_FLAG_ANY || pls->pkt->flags & AV_PKT_FLAG_KEY)) { .... }
+ pls->seek_timestamp = AV_NOPTS_VALUE; + break; + } + tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, tb.den, AV_ROUND_DOWN) -
BTW, when I used seek backward before, what I want is to get exactly packets of the specified seek time, not only the specified stream but also all streams I need. To achieve this, I will put this patch lines before this if block, like this: if (pls->seek_timestamp == AV_NOPTS_VALUE) break; if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD && (pls->seek_flags & AVSEEK_FLAG_ANY || pls->pkt->flags & AV_PKT_FLAG_KEY)) { .... } if (pls->seek_stream_index < 0 || ... Regards, Lingjiang Fang
On Fri, Jun 11, 2021 at 6:29 PM Lingjiang Fang <vacingfang@foxmail.com> wrote:
On Sat, 5 Jun 2021 22:42:26 +0200 Gustav Grusell <gustav.grusell@gmail.com> wrote:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485.
Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 9 +++++++++ 1 file changed, 9 insertions(+)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 8fc6924c90..fb8c9b071a 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2197,6 +2197,15 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) break; }
+ /* If AVSEEK_FLAG_BACKWARD set and not AVSEEK_FLAG_ANY, + * we return the first keyframe encountered */ + if (pls->seek_flags & AVSEEK_FLAG_BACKWARD && + !(pls->seek_flags & AVSEEK_FLAG_ANY) && + pls->pkt->flags & AV_PKT_FLAG_KEY) { taking account of the case of using flag backward and flag any together, a logic like this is better:
if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD && (pls->seek_flags & AVSEEK_FLAG_ANY || pls->pkt->flags & AV_PKT_FLAG_KEY)) { .... }
Thanks for your feedback! I'm not sure about this, I think it makes more sense if seeking with AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY behaves the same as when using only AVSEEK_FLAG_ANY, that is to seek to as close as possible as the exact timestamp, regardless if that is a keyframe or not. If I'm not mistaken, with your proposed change, using both AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY would always seek to the first frame of the segment which is always a keyframe, so behaviour would be the same as using only AVSEEK_FLAG_BACKWARD.
+ pls->seek_timestamp = AV_NOPTS_VALUE; + break; + } + tb = get_timebase(pls); ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, tb.den, AV_ROUND_DOWN) -
BTW, when I used seek backward before, what I want is to get exactly packets of the specified seek time, not only the specified stream but also all streams I need. To achieve this, I will put this patch lines before this if block, like this:
if (pls->seek_timestamp == AV_NOPTS_VALUE) break;
if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD && (pls->seek_flags & AVSEEK_FLAG_ANY || pls->pkt->flags & AV_PKT_FLAG_KEY)) { .... }
if (pls->seek_stream_index < 0 || ...
Not sure I'm following. If I'm not mistaken, the hls spec does not require audio and video segments to be aligned. Given this, it seems to me that the above proposed change could lead to incorrect seek for some streams when using AVSEEK_FLAG_BACKWARDS, since we would get the first frame of the segments for all streams, and for audio streams this might not match the timestamp of the first segment of the video stream. Cheers, Gustav Grusell
On Sat, 12 Jun 2021 14:16:22 +0200
Gustav Grusell <gustav.grusell@gmail.com> wrote:
> On Fri, Jun 11, 2021 at 6:29 PM Lingjiang Fang
> <vacingfang@foxmail.com> wrote:
>
> > On Sat, 5 Jun 2021 22:42:26 +0200
> > Gustav Grusell <gustav.grusell@gmail.com> wrote:
> >
> > > Before, seeking in hls streams would always seek to the next
> > > keyframe after the given timestamp. With this fix, if
> > > AVSEEK_FLAG_BACKWARD is set, seeking will be to the first
> > > keyframe of the segment containing the given timestamp. This
> > > fixes #7485.
> > >
> > > Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com>
> > > ---
> > > libavformat/hls.c | 9 +++++++++
> > > 1 file changed, 9 insertions(+)
> > >
> > > diff --git a/libavformat/hls.c b/libavformat/hls.c
> > > index 8fc6924c90..fb8c9b071a 100644
> > > --- a/libavformat/hls.c
> > > +++ b/libavformat/hls.c
> > > @@ -2197,6 +2197,15 @@ static int hls_read_packet(AVFormatContext
> > > *s, AVPacket *pkt) break;
> > > }
> > >
> > > + /* If AVSEEK_FLAG_BACKWARD set and not
> > > AVSEEK_FLAG_ANY,
> > > + * we return the first keyframe encountered
> > > */
> > > + if (pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > > + !(pls->seek_flags & AVSEEK_FLAG_ANY) &&
> > > + pls->pkt->flags & AV_PKT_FLAG_KEY) {
> > taking account of the case of using flag backward and flag any
> > together, a logic like this is better:
> >
> > if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > (pls->seek_flags & AVSEEK_FLAG_ANY ||
> > pls->pkt->flags & AV_PKT_FLAG_KEY)) {
> > ....
> > }
> >
> >
> Thanks for your feedback!
> I'm not sure about this, I think it makes more sense if seeking with
> AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY behaves the same as when
> using only AVSEEK_FLAG_ANY, that is to seek to as close as possible
> as the exact timestamp, regardless if that is a keyframe or not. If
what do you mean "AVSEEK_FLAG_BACKWARD and AVSEEK_FLAGAV_ANY behaves the
same as when using only AVSEEK_FLAG_ANY"?
I think the default behavior of seek is seek to key frame, where
AVSEEK_FLAGAV_ANY means we can accept seeking to non-key frame.
> I'm not mistaken, with your proposed change, using both
> AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY would always seek to the
> first frame of the segment which is always a keyframe, so behaviour
> would be the same as using only AVSEEK_FLAG_BACKWARD.
when the first packet of a segment is not key-frame(rarely seen, but
is possible), the behavior will be different.
- AVSEEK_FLAG_BACKWARD will go on seeking until find a key frame
- AVSEEK_FLAG_BACKWARD + AVSEEK_FLAG_BACKWARD should return immediatly
there is a similar logic at the end of this if{} block.
>
>
> > > + pls->seek_timestamp = AV_NOPTS_VALUE;
> > > + break;
> > > + }
> > > +
> > > tb = get_timebase(pls);
> > > ts_diff = av_rescale_rnd(pls->pkt->dts,
> > > AV_TIME_BASE, tb.den, AV_ROUND_DOWN) -
> >
> > BTW, when I used seek backward before, what I want is to get exactly
> > packets of the specified seek time, not only the specified stream
> > but also all streams I need. To achieve this, I will put this patch
> > lines before this if block, like this:
> >
> > if (pls->seek_timestamp == AV_NOPTS_VALUE)
> > break;
> >
> > if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > (pls->seek_flags & AVSEEK_FLAG_ANY ||
> > pls->pkt->flags & AV_PKT_FLAG_KEY)) {
> > ....
> > }
> >
> > if (pls->seek_stream_index < 0 ||
> > ...
> >
> >
> Not sure I'm following. If I'm not mistaken, the hls spec does not
> require audio and video segments to be aligned. Given this, it seems
> to me that the above proposed change could lead to incorrect seek for
> some streams when using AVSEEK_FLAG_BACKWARDS, since we would get the
> first frame of the segments for all streams, and for audio streams
> this might not match the timestamp of the first segment of the video
> stream.
it's hard to seek precisely when we want to seek backward, we just
guarantee we seek to a place before the given time, like what this
patch does.
the way I proposed is to follow the flag as much as possible, and behave
similarly no matter audio and video are in different playlists or in
one, because it only affects the stream in the same playlist.
anyway, it's just a suggestion, hoping more discussion :)
>
> Cheers,
> Gustav Grusell
> _______________________________________________
> 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".
Regards,
Lingjiang Fang
On Sat, Jun 12, 2021 at 6:51 PM Lingjiang Fang <vacingfang@foxmail.com>
wrote:
> On Sat, 12 Jun 2021 14:16:22 +0200
> Gustav Grusell <gustav.grusell@gmail.com> wrote:
>
> > On Fri, Jun 11, 2021 at 6:29 PM Lingjiang Fang
> > <vacingfang@foxmail.com> wrote:
> >
> > > On Sat, 5 Jun 2021 22:42:26 +0200
> > > Gustav Grusell <gustav.grusell@gmail.com> wrote:
> > >
> > > > Before, seeking in hls streams would always seek to the next
> > > > keyframe after the given timestamp. With this fix, if
> > > > AVSEEK_FLAG_BACKWARD is set, seeking will be to the first
> > > > keyframe of the segment containing the given timestamp. This
> > > > fixes #7485.
> > > >
> > > > Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com>
> > > > ---
> > > > libavformat/hls.c | 9 +++++++++
> > > > 1 file changed, 9 insertions(+)
> > > >
> > > > diff --git a/libavformat/hls.c b/libavformat/hls.c
> > > > index 8fc6924c90..fb8c9b071a 100644
> > > > --- a/libavformat/hls.c
> > > > +++ b/libavformat/hls.c
> > > > @@ -2197,6 +2197,15 @@ static int hls_read_packet(AVFormatContext
> > > > *s, AVPacket *pkt) break;
> > > > }
> > > >
> > > > + /* If AVSEEK_FLAG_BACKWARD set and not
> > > > AVSEEK_FLAG_ANY,
> > > > + * we return the first keyframe encountered
> > > > */
> > > > + if (pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > > > + !(pls->seek_flags & AVSEEK_FLAG_ANY) &&
> > > > + pls->pkt->flags & AV_PKT_FLAG_KEY) {
> > > taking account of the case of using flag backward and flag any
> > > together, a logic like this is better:
> > >
> > > if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > > (pls->seek_flags & AVSEEK_FLAG_ANY ||
> > > pls->pkt->flags & AV_PKT_FLAG_KEY)) {
> > > ....
> > > }
> > >
> > >
> > Thanks for your feedback!
> > I'm not sure about this, I think it makes more sense if seeking with
> > AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY behaves the same as when
> > using only AVSEEK_FLAG_ANY, that is to seek to as close as possible
> > as the exact timestamp, regardless if that is a keyframe or not. If
> what do you mean "AVSEEK_FLAG_BACKWARD and AVSEEK_FLAGAV_ANY behaves the
> same as when using only AVSEEK_FLAG_ANY"?
> I think the default behavior of seek is seek to key frame, where
> AVSEEK_FLAGAV_ANY means we can accept seeking to non-key frame.
>
>
Sorry I think you can disregard what I wrote. I was thinking of the case
where the user would set both BACKWARDS and ANY flags, and
forgot that if the user sets the BACKWARDS flag, the ANY flag will be added
for other playlists than the one containing the stream
searched in. I think this made me miss your point.
So what I meant was that if seeking with both AVSEEK_FLAG_BACKWARD and
AVSEEK_FLAG_ANY, optimally, I would expect the result to
be to search to the nearest frame before or exactly at the seek timestamp.
Note that this is not how i behaves with my patch, it will
seek to the first frame with a timestamp equal to or greater to the seek
timestamp. In my opinion this is probably good enough, and is
in any case the same behaviour as before the patch.
So if the user has etAVSEEK_FLAG_ANY we would not then want to search to
the first frame of the segment, even if we have the AVSEEK_FLAG_BACKWARD
set. But of course this is different if AVSEEK_FLAG_ANY has been set
because this is not the seek playlist.
> > I'm not mistaken, with your proposed change, using both
> > AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY would always seek to the
> > first frame of the segment which is always a keyframe, so behaviour
> > would be the same as using only AVSEEK_FLAG_BACKWARD.
> when the first packet of a segment is not key-frame(rarely seen, but
> is possible), the behavior will be different.
> - AVSEEK_FLAG_BACKWARD will go on seeking until find a key frame
> - AVSEEK_FLAG_BACKWARD + AVSEEK_FLAG_BACKWARD should return immediatly
>
>
Ah yes you are correct. I was thinking that the HLS specification requires
an IDR frame at the start of every (video) segment, but now that I looked
it up that is not strictly the case.
Apples HLS authorign specification does require it though.
there is a similar logic at the end of this if{} block.
>
> >
> >
> > > > + pls->seek_timestamp = AV_NOPTS_VALUE;
> > > > + break;
> > > > + }
> > > > +
> > > > tb = get_timebase(pls);
> > > > ts_diff = av_rescale_rnd(pls->pkt->dts,
> > > > AV_TIME_BASE, tb.den, AV_ROUND_DOWN) -
> > >
> > > BTW, when I used seek backward before, what I want is to get exactly
> > > packets of the specified seek time, not only the specified stream
> > > but also all streams I need. To achieve this, I will put this patch
> > > lines before this if block, like this:
> > >
> > > if (pls->seek_timestamp == AV_NOPTS_VALUE)
> > > break;
> > >
> > > if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > > (pls->seek_flags & AVSEEK_FLAG_ANY ||
> > > pls->pkt->flags & AV_PKT_FLAG_KEY)) {
> > > ....
> > > }
> > >
> > > if (pls->seek_stream_index < 0 ||
> > > ...
> > >
> > >
> > Not sure I'm following. If I'm not mistaken, the hls spec does not
> > require audio and video segments to be aligned. Given this, it seems
> > to me that the above proposed change could lead to incorrect seek for
> > some streams when using AVSEEK_FLAG_BACKWARDS, since we would get the
> > first frame of the segments for all streams, and for audio streams
> > this might not match the timestamp of the first segment of the video
> > stream.
> it's hard to seek precisely when we want to seek backward, we just
> guarantee we seek to a place before the given time, like what this
> patch does.
> the way I proposed is to follow the flag as much as possible, and behave
> similarly no matter audio and video are in different playlists or in
> one, because it only affects the stream in the same playlist.
>
Ah, it took me a while, but now I just realized you have a very valid
point. If audio is in different playlist than video and we seek in video
stream, the audio playlist will seek
with AVSEEK_FLAG_ANY and with my patch may seek to a timestamp that comes
quite some time (a few seconds or so) after the timestamp we seeked to in
the video stream. This seems undesirable, I will rethink and rewrite the
patch.
>
> anyway, it's just a suggestion, hoping more discussion :)
>
>
Many thanks for your input, very helpful :) .
Cheers,
Gustav
On Sat, 12 Jun 2021 23:14:16 +0200
Gustav Grusell <gustav.grusell@gmail.com> wrote:
> On Sat, Jun 12, 2021 at 6:51 PM Lingjiang Fang
> <vacingfang@foxmail.com> wrote:
>
> > On Sat, 12 Jun 2021 14:16:22 +0200
> > Gustav Grusell <gustav.grusell@gmail.com> wrote:
> >
> > > On Fri, Jun 11, 2021 at 6:29 PM Lingjiang Fang
> > > <vacingfang@foxmail.com> wrote:
> > >
> > > > On Sat, 5 Jun 2021 22:42:26 +0200
> > > > Gustav Grusell <gustav.grusell@gmail.com> wrote:
> > > >
> > > > > Before, seeking in hls streams would always seek to the next
> > > > > keyframe after the given timestamp. With this fix, if
> > > > > AVSEEK_FLAG_BACKWARD is set, seeking will be to the first
> > > > > keyframe of the segment containing the given timestamp. This
> > > > > fixes #7485.
> > > > >
> > > > > Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com>
> > > > > ---
> > > > > libavformat/hls.c | 9 +++++++++
> > > > > 1 file changed, 9 insertions(+)
> > > > >
> > > > > diff --git a/libavformat/hls.c b/libavformat/hls.c
> > > > > index 8fc6924c90..fb8c9b071a 100644
> > > > > --- a/libavformat/hls.c
> > > > > +++ b/libavformat/hls.c
> > > > > @@ -2197,6 +2197,15 @@ static int
> > > > > hls_read_packet(AVFormatContext *s, AVPacket *pkt) break;
> > > > > }
> > > > >
> > > > > + /* If AVSEEK_FLAG_BACKWARD set and not
> > > > > AVSEEK_FLAG_ANY,
> > > > > + * we return the first keyframe
> > > > > encountered */
> > > > > + if (pls->seek_flags &
> > > > > AVSEEK_FLAG_BACKWARD &&
> > > > > + !(pls->seek_flags & AVSEEK_FLAG_ANY)
> > > > > &&
> > > > > + pls->pkt->flags & AV_PKT_FLAG_KEY) {
> > > > >
> > > > taking account of the case of using flag backward and flag any
> > > > together, a logic like this is better:
> > > >
> > > > if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > > > (pls->seek_flags & AVSEEK_FLAG_ANY ||
> > > > pls->pkt->flags & AV_PKT_FLAG_KEY)) {
> > > > ....
> > > > }
> > > >
> > > >
> > > Thanks for your feedback!
> > > I'm not sure about this, I think it makes more sense if seeking
> > > with AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY behaves the same as
> > > when using only AVSEEK_FLAG_ANY, that is to seek to as close as
> > > possible as the exact timestamp, regardless if that is a keyframe
> > > or not. If
> > what do you mean "AVSEEK_FLAG_BACKWARD and AVSEEK_FLAGAV_ANY
> > behaves the same as when using only AVSEEK_FLAG_ANY"?
> > I think the default behavior of seek is seek to key frame, where
> > AVSEEK_FLAGAV_ANY means we can accept seeking to non-key frame.
> >
> >
> Sorry I think you can disregard what I wrote. I was thinking of the
> case where the user would set both BACKWARDS and ANY flags, and
> forgot that if the user sets the BACKWARDS flag, the ANY flag will be
> added for other playlists than the one containing the stream
> searched in. I think this made me miss your point.
> So what I meant was that if seeking with both AVSEEK_FLAG_BACKWARD and
> AVSEEK_FLAG_ANY, optimally, I would expect the result to
> be to search to the nearest frame before or exactly at the seek
> timestamp. Note that this is not how i behaves with my patch, it will
> seek to the first frame with a timestamp equal to or greater to the
> seek timestamp. In my opinion this is probably good enough, and is
> in any case the same behaviour as before the patch.
> So if the user has etAVSEEK_FLAG_ANY we would not then want to search
> to the first frame of the segment, even if we have the
> AVSEEK_FLAG_BACKWARD set. But of course this is different if
> AVSEEK_FLAG_ANY has been set because this is not the seek playlist.
>
>
> > > I'm not mistaken, with your proposed change, using both
> > > AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY would always seek to the
> > > first frame of the segment which is always a keyframe, so
> > > behaviour would be the same as using only AVSEEK_FLAG_BACKWARD.
> > when the first packet of a segment is not key-frame(rarely seen, but
> > is possible), the behavior will be different.
> > - AVSEEK_FLAG_BACKWARD will go on seeking until find a key frame
> > - AVSEEK_FLAG_BACKWARD + AVSEEK_FLAG_BACKWARD should return
> > immediatly
> >
> >
> Ah yes you are correct. I was thinking that the HLS specification
> requires an IDR frame at the start of every (video) segment, but now
> that I looked it up that is not strictly the case.
> Apples HLS authorign specification does require it though.
>
>
> there is a similar logic at the end of this if{} block.
> >
> > >
> > >
> > > > > + pls->seek_timestamp = AV_NOPTS_VALUE;
> > > > > + break;
> > > > > + }
> > > > > +
> > > > > tb = get_timebase(pls);
> > > > > ts_diff = av_rescale_rnd(pls->pkt->dts,
> > > > > AV_TIME_BASE, tb.den, AV_ROUND_DOWN) -
> > > >
> > > > BTW, when I used seek backward before, what I want is to get
> > > > exactly packets of the specified seek time, not only the
> > > > specified stream but also all streams I need. To achieve this,
> > > > I will put this patch lines before this if block, like this:
> > > >
> > > > if (pls->seek_timestamp == AV_NOPTS_VALUE)
> > > > break;
> > > >
> > > > if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > > > (pls->seek_flags & AVSEEK_FLAG_ANY ||
> > > > pls->pkt->flags & AV_PKT_FLAG_KEY)) {
> > > > ....
> > > > }
> > > >
> > > > if (pls->seek_stream_index < 0 ||
> > > > ...
> > > >
> > > >
> > > Not sure I'm following. If I'm not mistaken, the hls spec does not
> > > require audio and video segments to be aligned. Given this, it
> > > seems to me that the above proposed change could lead to
> > > incorrect seek for some streams when using AVSEEK_FLAG_BACKWARDS,
> > > since we would get the first frame of the segments for all
> > > streams, and for audio streams this might not match the timestamp
> > > of the first segment of the video stream.
> > it's hard to seek precisely when we want to seek backward, we just
> > guarantee we seek to a place before the given time, like what this
> > patch does.
> > the way I proposed is to follow the flag as much as possible, and
> > behave similarly no matter audio and video are in different
> > playlists or in one, because it only affects the stream in the same
> > playlist.
>
> Ah, it took me a while, but now I just realized you have a very valid
> point. If audio is in different playlist than video and we seek in
> video stream, the audio playlist will seek
> with AVSEEK_FLAG_ANY and with my patch may seek to a timestamp that
> comes quite some time (a few seconds or so) after the timestamp we
> seeked to in the video stream. This seems undesirable, I will rethink
> and rewrite the patch.
sorry, I didn't catch your point, can you give an example :)
BTW, summary of my view:
if I have more data, I can drop it; but if I missed something, I can't
make up it.
>
>
> >
> > anyway, it's just a suggestion, hoping more discussion :)
> >
> >
> Many thanks for your input, very helpful :) .
>
> Cheers,
> Gustav
> _______________________________________________
> 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".
Regards,
Lingjiang Fang
On Sun, Jun 13, 2021 at 5:14 PM Lingjiang Fang <vacingfang@hotmail.com>
wrote:
> On Sat, 12 Jun 2021 23:14:16 +0200
> Gustav Grusell <gustav.grusell@gmail.com> wrote:
>
> > On Sat, Jun 12, 2021 at 6:51 PM Lingjiang Fang
> > <vacingfang@foxmail.com> wrote:
> >
> > > On Sat, 12 Jun 2021 14:16:22 +0200
> > > Gustav Grusell <gustav.grusell@gmail.com> wrote:
> > >
> > > > On Fri, Jun 11, 2021 at 6:29 PM Lingjiang Fang
> > > > <vacingfang@foxmail.com> wrote:
> > > >
> > > > > On Sat, 5 Jun 2021 22:42:26 +0200
> > > > > Gustav Grusell <gustav.grusell@gmail.com> wrote:
> > > > >
> > > > > > Before, seeking in hls streams would always seek to the next
> > > > > > keyframe after the given timestamp. With this fix, if
> > > > > > AVSEEK_FLAG_BACKWARD is set, seeking will be to the first
> > > > > > keyframe of the segment containing the given timestamp. This
> > > > > > fixes #7485.
> > > > > >
> > > > > > Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com>
> > > > > > ---
> > > > > > libavformat/hls.c | 9 +++++++++
> > > > > > 1 file changed, 9 insertions(+)
> > > > > >
> > > > > > diff --git a/libavformat/hls.c b/libavformat/hls.c
> > > > > > index 8fc6924c90..fb8c9b071a 100644
> > > > > > --- a/libavformat/hls.c
> > > > > > +++ b/libavformat/hls.c
> > > > > > @@ -2197,6 +2197,15 @@ static int
> > > > > > hls_read_packet(AVFormatContext *s, AVPacket *pkt) break;
> > > > > > }
> > > > > >
> > > > > > + /* If AVSEEK_FLAG_BACKWARD set and not
> > > > > > AVSEEK_FLAG_ANY,
> > > > > > + * we return the first keyframe
> > > > > > encountered */
> > > > > > + if (pls->seek_flags &
> > > > > > AVSEEK_FLAG_BACKWARD &&
> > > > > > + !(pls->seek_flags & AVSEEK_FLAG_ANY)
> > > > > > &&
> > > > > > + pls->pkt->flags & AV_PKT_FLAG_KEY) {
> > > > > >
> > > > > taking account of the case of using flag backward and flag any
> > > > > together, a logic like this is better:
> > > > >
> > > > > if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > > > > (pls->seek_flags & AVSEEK_FLAG_ANY ||
> > > > > pls->pkt->flags & AV_PKT_FLAG_KEY)) {
> > > > > ....
> > > > > }
> > > > >
> > > > >
> > > > Thanks for your feedback!
> > > > I'm not sure about this, I think it makes more sense if seeking
> > > > with AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY behaves the same as
> > > > when using only AVSEEK_FLAG_ANY, that is to seek to as close as
> > > > possible as the exact timestamp, regardless if that is a keyframe
> > > > or not. If
> > > what do you mean "AVSEEK_FLAG_BACKWARD and AVSEEK_FLAGAV_ANY
> > > behaves the same as when using only AVSEEK_FLAG_ANY"?
> > > I think the default behavior of seek is seek to key frame, where
> > > AVSEEK_FLAGAV_ANY means we can accept seeking to non-key frame.
> > >
> > >
> > Sorry I think you can disregard what I wrote. I was thinking of the
> > case where the user would set both BACKWARDS and ANY flags, and
> > forgot that if the user sets the BACKWARDS flag, the ANY flag will be
> > added for other playlists than the one containing the stream
> > searched in. I think this made me miss your point.
> > So what I meant was that if seeking with both AVSEEK_FLAG_BACKWARD and
> > AVSEEK_FLAG_ANY, optimally, I would expect the result to
> > be to search to the nearest frame before or exactly at the seek
> > timestamp. Note that this is not how i behaves with my patch, it will
> > seek to the first frame with a timestamp equal to or greater to the
> > seek timestamp. In my opinion this is probably good enough, and is
> > in any case the same behaviour as before the patch.
> > So if the user has etAVSEEK_FLAG_ANY we would not then want to search
> > to the first frame of the segment, even if we have the
> > AVSEEK_FLAG_BACKWARD set. But of course this is different if
> > AVSEEK_FLAG_ANY has been set because this is not the seek playlist.
> >
> >
> > > > I'm not mistaken, with your proposed change, using both
> > > > AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY would always seek to the
> > > > first frame of the segment which is always a keyframe, so
> > > > behaviour would be the same as using only AVSEEK_FLAG_BACKWARD.
> > > when the first packet of a segment is not key-frame(rarely seen, but
> > > is possible), the behavior will be different.
> > > - AVSEEK_FLAG_BACKWARD will go on seeking until find a key frame
> > > - AVSEEK_FLAG_BACKWARD + AVSEEK_FLAG_BACKWARD should return
> > > immediatly
> > >
> > >
> > Ah yes you are correct. I was thinking that the HLS specification
> > requires an IDR frame at the start of every (video) segment, but now
> > that I looked it up that is not strictly the case.
> > Apples HLS authorign specification does require it though.
> >
> >
> > there is a similar logic at the end of this if{} block.
> > >
> > > >
> > > >
> > > > > > + pls->seek_timestamp = AV_NOPTS_VALUE;
> > > > > > + break;
> > > > > > + }
> > > > > > +
> > > > > > tb = get_timebase(pls);
> > > > > > ts_diff = av_rescale_rnd(pls->pkt->dts,
> > > > > > AV_TIME_BASE, tb.den, AV_ROUND_DOWN) -
> > > > >
> > > > > BTW, when I used seek backward before, what I want is to get
> > > > > exactly packets of the specified seek time, not only the
> > > > > specified stream but also all streams I need. To achieve this,
> > > > > I will put this patch lines before this if block, like this:
> > > > >
> > > > > if (pls->seek_timestamp == AV_NOPTS_VALUE)
> > > > > break;
> > > > >
> > > > > if ( pls->seek_flags & AVSEEK_FLAG_BACKWARD &&
> > > > > (pls->seek_flags & AVSEEK_FLAG_ANY ||
> > > > > pls->pkt->flags & AV_PKT_FLAG_KEY)) {
> > > > > ....
> > > > > }
> > > > >
> > > > > if (pls->seek_stream_index < 0 ||
> > > > > ...
> > > > >
> > > > >
> > > > Not sure I'm following. If I'm not mistaken, the hls spec does not
> > > > require audio and video segments to be aligned. Given this, it
> > > > seems to me that the above proposed change could lead to
> > > > incorrect seek for some streams when using AVSEEK_FLAG_BACKWARDS,
> > > > since we would get the first frame of the segments for all
> > > > streams, and for audio streams this might not match the timestamp
> > > > of the first segment of the video stream.
> > > it's hard to seek precisely when we want to seek backward, we just
> > > guarantee we seek to a place before the given time, like what this
> > > patch does.
> > > the way I proposed is to follow the flag as much as possible, and
> > > behave similarly no matter audio and video are in different
> > > playlists or in one, because it only affects the stream in the same
> > > playlist.
> >
> > Ah, it took me a while, but now I just realized you have a very valid
> > point. If audio is in different playlist than video and we seek in
> > video stream, the audio playlist will seek
> > with AVSEEK_FLAG_ANY and with my patch may seek to a timestamp that
> > comes quite some time (a few seconds or so) after the timestamp we
> > seeked to in the video stream. This seems undesirable, I will rethink
> > and rewrite the patch.
> sorry, I didn't catch your point, can you give an example :)
>
>
Suppose you have a hls manifest with separate playlists for video and
audio. Video and audio are not aligned, suppose video segments have a
duration of 4s and audio segments have a duration of 3.97s. Then say you
want to seek to 163s from the start of the video. For the video, this would
be in the 41th segment, that starts at 160s. For audio, the seek point
would be in the 42nd segment, that starts at 162.77. With my patch for
seeking (with or without your amendments) both video and audio would seek
to the beginning of the segment containing the timestamp 163s from start.
This would mean that after seeking the video stream would be at 160s, and
the audio stream at 162.77, so we would miss some audio packages.
> BTW, summary of my view:
> if I have more data, I can drop it; but if I missed something, I can't
> make up it.
>
> Ah yes, that makes sense.
Cheers,
Gustav Grusell
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if seeking in videostream and AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485. Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/libavformat/hls.c b/libavformat/hls.c index 4568e72cb2..44afdaab42 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -1653,7 +1653,8 @@ static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pl /* if timestamp was in valid range: returns 1 and sets seq_no * if not: returns 0 and sets seq_no to closest segment */ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, - int64_t timestamp, int64_t *seq_no) + int64_t timestamp, int64_t *seq_no, + int64_t *seg_start_ts) { int i; int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? @@ -1668,6 +1669,9 @@ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, int64_t diff = pos + pls->segments[i]->duration - timestamp; if (diff > 0) { *seq_no = pls->start_seq_no + i; + if (seg_start_ts) { + *seg_start_ts = pos; + } return 1; } pos += pls->segments[i]->duration; @@ -1691,7 +1695,7 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) * playlist) and this is a complete file, find the matching segment * by counting durations. */ if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) { - find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no); + find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL); return seq_no; } @@ -2362,7 +2366,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, int i, j; int stream_subdemuxer_index; int64_t first_timestamp, seek_timestamp, duration; - int64_t seq_no; + int64_t seq_no, seg_start_ts; if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE)) return AVERROR(ENOSYS); @@ -2372,8 +2376,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, seek_timestamp = av_rescale_rnd(timestamp, AV_TIME_BASE, s->streams[stream_index]->time_base.den, - flags & AVSEEK_FLAG_BACKWARD ? - AV_ROUND_DOWN : AV_ROUND_UP); + AV_ROUND_DOWN); duration = s->duration == AV_NOPTS_VALUE ? 0 : s->duration; @@ -2394,9 +2397,16 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, } /* check if the timestamp is valid for the playlist with the * specified stream index */ - if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no)) + if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no, &seg_start_ts)) return AVERROR(EIO); + if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + flags & AVSEEK_FLAG_BACKWARD && !(flags & AVSEEK_FLAG_ANY)) { + /* Seeking to start of segment ensures we seek to a keyframe located + * before the given timestamp. */ + seek_timestamp = seg_start_ts; + } + /* set segment now so we do not need to search again below */ seek_pls->cur_seq_no = seq_no; seek_pls->seek_stream_index = stream_subdemuxer_index; @@ -2423,7 +2433,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, if (pls != seek_pls) { /* set closest segment seq_no for playlists not handled above */ - find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no); + find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no, NULL); /* seek the playlist to the given position without taking * keyframes into account since this playlist does not have the * specified stream where we should look for the keyframes */ -- 2.25.1
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if seeking in videostream and AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485. Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/libavformat/hls.c b/libavformat/hls.c index 4568e72cb2..44afdaab42 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -1653,7 +1653,8 @@ static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pl /* if timestamp was in valid range: returns 1 and sets seq_no * if not: returns 0 and sets seq_no to closest segment */ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, - int64_t timestamp, int64_t *seq_no) + int64_t timestamp, int64_t *seq_no, + int64_t *seg_start_ts) { int i; int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? @@ -1668,6 +1669,9 @@ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, int64_t diff = pos + pls->segments[i]->duration - timestamp; if (diff > 0) { *seq_no = pls->start_seq_no + i; + if (seg_start_ts) { + *seg_start_ts = pos; + } return 1; } pos += pls->segments[i]->duration; @@ -1691,7 +1695,7 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) * playlist) and this is a complete file, find the matching segment * by counting durations. */ if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) { - find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no); + find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL); return seq_no; } @@ -2362,7 +2366,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, int i, j; int stream_subdemuxer_index; int64_t first_timestamp, seek_timestamp, duration; - int64_t seq_no; + int64_t seq_no, seg_start_ts; if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE)) return AVERROR(ENOSYS); @@ -2372,8 +2376,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, seek_timestamp = av_rescale_rnd(timestamp, AV_TIME_BASE, s->streams[stream_index]->time_base.den, - flags & AVSEEK_FLAG_BACKWARD ? - AV_ROUND_DOWN : AV_ROUND_UP); + AV_ROUND_DOWN); duration = s->duration == AV_NOPTS_VALUE ? 0 : s->duration; @@ -2394,9 +2397,16 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, } /* check if the timestamp is valid for the playlist with the * specified stream index */ - if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no)) + if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no, &seg_start_ts)) return AVERROR(EIO); + if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + flags & AVSEEK_FLAG_BACKWARD && !(flags & AVSEEK_FLAG_ANY)) { + /* Seeking to start of segment ensures we seek to a keyframe located + * before the given timestamp. */ + seek_timestamp = seg_start_ts; + } + /* set segment now so we do not need to search again below */ seek_pls->cur_seq_no = seq_no; seek_pls->seek_stream_index = stream_subdemuxer_index; @@ -2423,7 +2433,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, if (pls != seek_pls) { /* set closest segment seq_no for playlists not handled above */ - find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no); + find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no, NULL); /* seek the playlist to the given position without taking * keyframes into account since this playlist does not have the * specified stream where we should look for the keyframes */ -- 2.25.1
Any comments on this? cheers, Gustav On Fri, Jan 28, 2022 at 9:24 PM Gustav Grusell <gustav.grusell@gmail.com> wrote:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if seeking in videostream and AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485.
Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 4568e72cb2..44afdaab42 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -1653,7 +1653,8 @@ static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pl /* if timestamp was in valid range: returns 1 and sets seq_no * if not: returns 0 and sets seq_no to closest segment */ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, - int64_t timestamp, int64_t *seq_no) + int64_t timestamp, int64_t *seq_no, + int64_t *seg_start_ts) { int i; int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? @@ -1668,6 +1669,9 @@ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, int64_t diff = pos + pls->segments[i]->duration - timestamp; if (diff > 0) { *seq_no = pls->start_seq_no + i; + if (seg_start_ts) { + *seg_start_ts = pos; + } return 1; } pos += pls->segments[i]->duration; @@ -1691,7 +1695,7 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) * playlist) and this is a complete file, find the matching segment * by counting durations. */ if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) { - find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no); + find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL); return seq_no; }
@@ -2362,7 +2366,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, int i, j; int stream_subdemuxer_index; int64_t first_timestamp, seek_timestamp, duration; - int64_t seq_no; + int64_t seq_no, seg_start_ts;
if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE)) return AVERROR(ENOSYS); @@ -2372,8 +2376,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
seek_timestamp = av_rescale_rnd(timestamp, AV_TIME_BASE,
s->streams[stream_index]->time_base.den, - flags & AVSEEK_FLAG_BACKWARD ? - AV_ROUND_DOWN : AV_ROUND_UP); + AV_ROUND_DOWN);
duration = s->duration == AV_NOPTS_VALUE ? 0 : s->duration; @@ -2394,9 +2397,16 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, } /* check if the timestamp is valid for the playlist with the * specified stream index */ - if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no)) + if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no, &seg_start_ts)) return AVERROR(EIO);
+ if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + flags & AVSEEK_FLAG_BACKWARD && !(flags & AVSEEK_FLAG_ANY)) { + /* Seeking to start of segment ensures we seek to a keyframe located + * before the given timestamp. */ + seek_timestamp = seg_start_ts; + } + /* set segment now so we do not need to search again below */ seek_pls->cur_seq_no = seq_no; seek_pls->seek_stream_index = stream_subdemuxer_index; @@ -2423,7 +2433,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
if (pls != seek_pls) { /* set closest segment seq_no for playlists not handled above */ - find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no); + find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no, NULL); /* seek the playlist to the given position without taking * keyframes into account since this playlist does not have the * specified stream where we should look for the keyframes */ -- 2.25.1
2022年1月29日 上午4:23,Gustav Grusell <gustav.grusell@gmail.com> 写道:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if seeking in videostream and AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485.
Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 4568e72cb2..44afdaab42 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -1653,7 +1653,8 @@ static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pl /* if timestamp was in valid range: returns 1 and sets seq_no * if not: returns 0 and sets seq_no to closest segment */ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, - int64_t timestamp, int64_t *seq_no) + int64_t timestamp, int64_t *seq_no, + int64_t *seg_start_ts) { int i; int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? @@ -1668,6 +1669,9 @@ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, int64_t diff = pos + pls->segments[i]->duration - timestamp; if (diff > 0) { *seq_no = pls->start_seq_no + i; + if (seg_start_ts) { + *seg_start_ts = pos; + } return 1; } pos += pls->segments[i]->duration; @@ -1691,7 +1695,7 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) * playlist) and this is a complete file, find the matching segment * by counting durations. */ if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) { - find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no); + find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL); return seq_no; }
@@ -2362,7 +2366,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, int i, j; int stream_subdemuxer_index; int64_t first_timestamp, seek_timestamp, duration; - int64_t seq_no; + int64_t seq_no, seg_start_ts;
if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE)) return AVERROR(ENOSYS); @@ -2372,8 +2376,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
seek_timestamp = av_rescale_rnd(timestamp, AV_TIME_BASE, s->streams[stream_index]->time_base.den, - flags & AVSEEK_FLAG_BACKWARD ? - AV_ROUND_DOWN : AV_ROUND_UP); + AV_ROUND_DOWN);
duration = s->duration == AV_NOPTS_VALUE ? 0 : s->duration; @@ -2394,9 +2397,16 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, } /* check if the timestamp is valid for the playlist with the * specified stream index */ - if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no)) + if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no, &seg_start_ts)) return AVERROR(EIO);
+ if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + flags & AVSEEK_FLAG_BACKWARD && !(flags & AVSEEK_FLAG_ANY)) { + /* Seeking to start of segment ensures we seek to a keyframe located + * before the given timestamp. */ + seek_timestamp = seg_start_ts; + } + /* set segment now so we do not need to search again below */ seek_pls->cur_seq_no = seq_no; seek_pls->seek_stream_index = stream_subdemuxer_index; @@ -2423,7 +2433,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
if (pls != seek_pls) { /* set closest segment seq_no for playlists not handled above */ - find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no); + find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no, NULL); /* seek the playlist to the given position without taking * keyframes into account since this playlist does not have the * specified stream where we should look for the keyframes */ -- 2.25.1
_______________________________________________ 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".
LGTM Thanks Steven Liu
Steven Liu <lq@chinaffmpeg.org> 于2022年2月3日周四 18:22写道:
2022年1月29日 上午4:23,Gustav Grusell <gustav.grusell@gmail.com> 写道:
Before, seeking in hls streams would always seek to the next keyframe after the given timestamp. With this fix, if seeking in videostream and AVSEEK_FLAG_BACKWARD is set, seeking will be to the first keyframe of the segment containing the given timestamp. This fixes #7485.
Signed-off-by: Gustav Grusell <gustav.grusell@gmail.com> --- libavformat/hls.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/libavformat/hls.c b/libavformat/hls.c index 4568e72cb2..44afdaab42 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -1653,7 +1653,8 @@ static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pl /* if timestamp was in valid range: returns 1 and sets seq_no * if not: returns 0 and sets seq_no to closest segment */ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, - int64_t timestamp, int64_t *seq_no) + int64_t timestamp, int64_t *seq_no, + int64_t *seg_start_ts) { int i; int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? @@ -1668,6 +1669,9 @@ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, int64_t diff = pos + pls->segments[i]->duration - timestamp; if (diff > 0) { *seq_no = pls->start_seq_no + i; + if (seg_start_ts) { + *seg_start_ts = pos; + } return 1; } pos += pls->segments[i]->duration; @@ -1691,7 +1695,7 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) * playlist) and this is a complete file, find the matching segment * by counting durations. */ if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) { - find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no); + find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL); return seq_no; }
@@ -2362,7 +2366,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, int i, j; int stream_subdemuxer_index; int64_t first_timestamp, seek_timestamp, duration; - int64_t seq_no; + int64_t seq_no, seg_start_ts;
if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE)) return AVERROR(ENOSYS); @@ -2372,8 +2376,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
seek_timestamp = av_rescale_rnd(timestamp, AV_TIME_BASE, s->streams[stream_index]->time_base.den, - flags & AVSEEK_FLAG_BACKWARD ? - AV_ROUND_DOWN : AV_ROUND_UP); + AV_ROUND_DOWN);
duration = s->duration == AV_NOPTS_VALUE ? 0 : s->duration; @@ -2394,9 +2397,16 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, } /* check if the timestamp is valid for the playlist with the * specified stream index */ - if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no)) + if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no, &seg_start_ts)) return AVERROR(EIO);
+ if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + flags & AVSEEK_FLAG_BACKWARD && !(flags & AVSEEK_FLAG_ANY)) { + /* Seeking to start of segment ensures we seek to a keyframe located + * before the given timestamp. */ + seek_timestamp = seg_start_ts; + } + /* set segment now so we do not need to search again below */ seek_pls->cur_seq_no = seq_no; seek_pls->seek_stream_index = stream_subdemuxer_index; @@ -2423,7 +2433,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
if (pls != seek_pls) { /* set closest segment seq_no for playlists not handled above */ - find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no); + find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no, NULL); /* seek the playlist to the given position without taking * keyframes into account since this playlist does not have the * specified stream where we should look for the keyframes */ -- 2.25.1
_______________________________________________ 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".
LGTM
Applied as e78d0810d1741535c95e5ae0f198977626b1cdff Thanks Steven
participants (7)
-
Gustav Grusell -
Lingjiang Fang -
Lingjiang Fang -
Steven Liu -
Steven Liu -
Valerii Zapodovnikov -
徐慧书