diff mbox series

[FFmpeg-devel,25/29] fftools/ffmpeg_demux: set the timebase on demuxed packets

Message ID 20230409140853.28858-25-anton@khirnov.net
State Accepted
Commit d1cb31d7d8a59f131d6ca2b7d5b4cf4dccc795cf
Headers show
Series [FFmpeg-devel,01/29] fftools/ffmpeg: move OutputStream.vsync_frame_number to Encoder | expand

Checks

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

Commit Message

Anton Khirnov April 9, 2023, 2:08 p.m. UTC
Simplifies tracking what timebase are the timestamps in. Will be useful
in following commits.
---
 fftools/ffmpeg_demux.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

Comments

James Almer April 12, 2023, 2:49 a.m. UTC | #1
On 4/9/2023 11:08 AM, Anton Khirnov wrote:
> Simplifies tracking what timebase are the timestamps in. Will be useful
> in following commits.
> ---
>   fftools/ffmpeg_demux.c | 16 +++++++++-------
>   1 file changed, 9 insertions(+), 7 deletions(-)
> 
> diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
> index f2da0826ad..7ff57273c9 100644
> --- a/fftools/ffmpeg_demux.c
> +++ b/fftools/ffmpeg_demux.c
> @@ -194,15 +194,17 @@ static void ts_fixup(Demuxer *d, AVPacket *pkt, int *repeat_pict)
>       const int64_t start_time = ifile->start_time_effective;
>       int64_t duration;
>   
> +    pkt->time_base = ist->st->time_base;

Might be a good future-proof idea to check first if pkt->time_base is 
already set or not. The doxy says demuxers themselves may start doing so 
at some point.

> +
>   #define SHOW_TS_DEBUG(tag_)                                             \
>       if (debug_ts) {                                                     \
>           av_log(ist, AV_LOG_INFO, "%s -> ist_index:%d:%d type:%s "       \
>                  "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n", \
>                  tag_, ifile->index, pkt->stream_index,                   \
>                  av_get_media_type_string(ist->st->codecpar->codec_type), \
> -               av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base), \
> -               av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base), \
> -               av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base)); \
> +               av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base), \
> +               av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base), \
> +               av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base)); \
>       }
>   
>       SHOW_TS_DEBUG("demuxer");
> @@ -211,7 +213,7 @@ static void ts_fixup(Demuxer *d, AVPacket *pkt, int *repeat_pict)
>           ist->st->pts_wrap_bits < 64) {
>           int64_t stime, stime2;
>   
> -        stime = av_rescale_q(start_time, AV_TIME_BASE_Q, ist->st->time_base);
> +        stime = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
>           stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
>           ist->wrap_correction_done = 1;
>   
> @@ -226,16 +228,16 @@ static void ts_fixup(Demuxer *d, AVPacket *pkt, int *repeat_pict)
>       }
>   
>       if (pkt->dts != AV_NOPTS_VALUE)
> -        pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
> +        pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
>       if (pkt->pts != AV_NOPTS_VALUE)
> -        pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
> +        pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
>   
>       if (pkt->pts != AV_NOPTS_VALUE)
>           pkt->pts *= ds->ts_scale;
>       if (pkt->dts != AV_NOPTS_VALUE)
>           pkt->dts *= ds->ts_scale;
>   
> -    duration = av_rescale_q(d->duration, d->time_base, ist->st->time_base);
> +    duration = av_rescale_q(d->duration, d->time_base, pkt->time_base);
>       if (pkt->pts != AV_NOPTS_VALUE) {
>           pkt->pts += duration;
>           ds->max_pts = FFMAX(pkt->pts, ds->max_pts);
Anton Khirnov April 12, 2023, 5:59 a.m. UTC | #2
Quoting James Almer (2023-04-12 04:49:00)
> On 4/9/2023 11:08 AM, Anton Khirnov wrote:
> > Simplifies tracking what timebase are the timestamps in. Will be useful
> > in following commits.
> > ---
> >   fftools/ffmpeg_demux.c | 16 +++++++++-------
> >   1 file changed, 9 insertions(+), 7 deletions(-)
> > 
> > diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
> > index f2da0826ad..7ff57273c9 100644
> > --- a/fftools/ffmpeg_demux.c
> > +++ b/fftools/ffmpeg_demux.c
> > @@ -194,15 +194,17 @@ static void ts_fixup(Demuxer *d, AVPacket *pkt, int *repeat_pict)
> >       const int64_t start_time = ifile->start_time_effective;
> >       int64_t duration;
> >   
> > +    pkt->time_base = ist->st->time_base;
> 
> Might be a good future-proof idea to check first if pkt->time_base is 
> already set or not. The doxy says demuxers themselves may start doing so 
> at some point.

Why would that make any difference? The timestamps have to be in the
stream timebase, anything else would be an API break. So whatever value
is or is not written there by the demuxer, we can always overwrite it by
what we know has to be the corect timebase.
diff mbox series

Patch

diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index f2da0826ad..7ff57273c9 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -194,15 +194,17 @@  static void ts_fixup(Demuxer *d, AVPacket *pkt, int *repeat_pict)
     const int64_t start_time = ifile->start_time_effective;
     int64_t duration;
 
+    pkt->time_base = ist->st->time_base;
+
 #define SHOW_TS_DEBUG(tag_)                                             \
     if (debug_ts) {                                                     \
         av_log(ist, AV_LOG_INFO, "%s -> ist_index:%d:%d type:%s "       \
                "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n", \
                tag_, ifile->index, pkt->stream_index,                   \
                av_get_media_type_string(ist->st->codecpar->codec_type), \
-               av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base), \
-               av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base), \
-               av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base)); \
+               av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base), \
+               av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base), \
+               av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base)); \
     }
 
     SHOW_TS_DEBUG("demuxer");
@@ -211,7 +213,7 @@  static void ts_fixup(Demuxer *d, AVPacket *pkt, int *repeat_pict)
         ist->st->pts_wrap_bits < 64) {
         int64_t stime, stime2;
 
-        stime = av_rescale_q(start_time, AV_TIME_BASE_Q, ist->st->time_base);
+        stime = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
         stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
         ist->wrap_correction_done = 1;
 
@@ -226,16 +228,16 @@  static void ts_fixup(Demuxer *d, AVPacket *pkt, int *repeat_pict)
     }
 
     if (pkt->dts != AV_NOPTS_VALUE)
-        pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
+        pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
     if (pkt->pts != AV_NOPTS_VALUE)
-        pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
+        pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
 
     if (pkt->pts != AV_NOPTS_VALUE)
         pkt->pts *= ds->ts_scale;
     if (pkt->dts != AV_NOPTS_VALUE)
         pkt->dts *= ds->ts_scale;
 
-    duration = av_rescale_q(d->duration, d->time_base, ist->st->time_base);
+    duration = av_rescale_q(d->duration, d->time_base, pkt->time_base);
     if (pkt->pts != AV_NOPTS_VALUE) {
         pkt->pts += duration;
         ds->max_pts = FFMAX(pkt->pts, ds->max_pts);