[FFmpeg-devel] [PATCH 1/2] lavf/subtitles: add ff_subtitles_queue_seek().

Michael Niedermayer michaelni at gmx.at
Mon Nov 26 03:39:58 CET 2012


On Mon, Nov 26, 2012 at 03:13:16AM +0100, Clément Bœsch wrote:
> On Sun, Nov 25, 2012 at 07:44:38PM +0100, Michael Niedermayer wrote:
> > On Fri, Nov 23, 2012 at 09:45:01PM +0100, Clément Bœsch wrote:
> > > This function is almost identical to lavf/assdec:read_seek2(). It
> > > performs a generic seek for text subtitles demuxers for the new seeking
> > > API.
> > > 
> > > The seek callback in the ASS demuxer will be removed when it is
> > > redesigned to use FFDemuxSubtitlesQueue.
> > > ---
> > >  libavformat/subtitles.c | 38 ++++++++++++++++++++++++++++++++++++++
> > >  libavformat/subtitles.h |  7 +++++++
> > >  2 files changed, 45 insertions(+)
> > > 
> > > diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
> > > index 1204526..3661495 100644
> > > --- a/libavformat/subtitles.c
> > > +++ b/libavformat/subtitles.c
> > > @@ -91,6 +91,44 @@ int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
> > >      return 0;
> > >  }
> > >  
> > > +int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
> > > +                            int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
> > > +{
> > > +    if (flags & AVSEEK_FLAG_BYTE) {
> > > +        return AVERROR(ENOSYS);
> > > +    } else if (flags & AVSEEK_FLAG_FRAME) {
> > > +        if (ts < 0 || ts >= q->nb_subs)
> > > +            return AVERROR(ERANGE);
> > > +        q->current_sub_idx = ts;
> > > +    } else {
> > > +        int i, idx = -1;
> > > +        int64_t min_ts_diff = INT64_MAX;
> > > +        if (stream_index == -1) {
> > > +            AVRational time_base = s->streams[0]->time_base;
> > > +            ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
> > > +            min_ts = av_rescale_rnd(min_ts, time_base.den,
> > > +                                    time_base.num * (int64_t)AV_TIME_BASE,
> > > +                                    AV_ROUND_UP);
> > > +            max_ts = av_rescale_rnd(max_ts, time_base.den,
> > > +                                    time_base.num * (int64_t)AV_TIME_BASE,
> > > +                                    AV_ROUND_DOWN);
> > > +        }
> > > +        /* TODO: q->subs[] is sorted by pts so we could do a binary search */
> > > +        for (i = 0; i < q->nb_subs; i++) {
> > > +            int64_t pts = q->subs[i].pts;
> > 
> > > +            int64_t ts_diff = FFABS(pts - ts);
> > 
> > this needs to be unsigned to avoid overflow
> > 
> > 
> > > +            if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
> > > +                min_ts_diff = ts_diff;
> > > +                idx = i;
> > > +            }
> > 
> > iam not sure this is correct, please correct me if i misunderstand
> > something.
> > consider there are subtitles, first one displays from 2 to 10 second
> > second one displays from 5 to 18 seconds
> > if you want to seek to second 8 you actually need to seek to 2 to
> > get both displayed at your target
> > 
> 
> I admit I didn't give much thoughts about it; as stated in the commit
> description I took this code from lavf/assdec:read_seek2(), added in
> ac066b83. The code is just adapted to work with FFDemuxSubtitlesQueue, and
> it appeared to work fine with my simple tests so I didn't look much into
> it.
> 
> Now if we want to honor the overlapping issue you're mentioning, we will
> likely need to introduce a small threshold, such as limit to 5 or 10
> seconds

> (imagine the case where a subtitle event from the beginning up to
> 1 hour, like a banner, branding or something).

> BTW, I don't know what are
> the consequences of a very incorrect seek with subtitles…

for a video with subtitles, you dont see the correct subtitles after
seeking or in case of -ss with ffmpeg they are permanently missing
in the transcoded file.
That assumes here that you flush the renderer, seek and then feed it
with the following subtitles.

If you consider a system where all the files subtitles are send to
the renderer at startup then this problem does not arrise in such case
that is the seeking function is unused. but this doesnt solve the
transcoding case and it only works when the container makes all
subititles efficiently available at startup

1 hour long subtitles could be split, for example on load or during
seeking virtually to start from the seeking position, or it could
simply be accepted that they are output as they are on seeking.


[...]


-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Its not that you shouldnt use gotos but rather that you should write
readable code and code with gotos often but not always is less readable
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121126/4525c1e2/attachment.asc>


More information about the ffmpeg-devel mailing list