[vlc-devel] [PATCH] mux/avformat: normalize pts and dts in AVPacket

Filip Roséen filip at atch.se
Tue Feb 7 17:14:06 CET 2017

Sorry for potential duplicates

It started out with me accidentally replying only to Rémi (and not the
*mailing-list*), which then lead me to *bounce* the email to
`vlc-devel` as I thought that was an awesome idea (tldr: it wasn't).

As it turns out, the bounce seems not to have made it through to some
subscribers of the list, and as such I now resend the same email one
more time.. this time with `To:` properly being set to
`vlc-devel at videolan.org`.

I deeply apologize for the inconvenience.


Hi Rémi,

On 2017-02-07 15:22, Rémi Denis-Courmont wrote:

> On February 7, 2017 2:15:21 PM GMT+02:00, "Filip Roséen" <filip at atch.se> wrote:
> >modules/demux/avformat/mux.c | 48
> >++++++++++++++++++++++++++++++++++----------
> > 1 file changed, 37 insertions(+), 11 deletions(-)
> Hello,
> AFAIK, the first block is not necessarily the one with the smalledt
> PTS and the smallest DTS. There might not even be such a block.
> So I guess, this patch may cause negative timestamps in certain corner cases.

Yes, I was thinking the same but when I did some testing it turns out
that negative timestamps are, to my initial surprise, handled
correctly by *avformat* (in terms of what it actually generates in the

They are adjusted to be starting at zero by `mux.c:write_packet`,
which is called from within `av_write_frame` (from my skimming of the
source code).

Link to documentation for the relevant function where the magic

 - https://ffmpeg.org/doxygen/3.2/mux_8c.html#a35b04d31f9156dd1dbca471d097741d8
 - https://ffmpeg.org/doxygen/3.2/mux_8c_source.html#l00693

I just tried with explicitly making the timestamps negative by
modifying the source code in the following way, and the generated
output is correct for the containers that I have tested.

    diff --git a/modules/demux/avformat/mux.c b/modules/demux/avformat/mux.c
    index b2b7bb467d..86cd284180 100644
    --- a/modules/demux/avformat/mux.c
    +++ b/modules/demux/avformat/mux.c
    @@ -376,11 +376,11 @@ static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
         if( p_data->i_pts > 0 )
             pkt.pts = NormalizeTS( &p_input_sys->base_ts.i_pts,
    -                                p_stream, p_data->i_pts );
    +                                p_stream, p_data->i_pts ) - 1013370;
         if( p_data->i_dts > 0 )
             pkt.dts = NormalizeTS( &p_input_sys->base_ts.i_dts,
    -                                p_stream, p_data->i_dts );
    +                                p_stream, p_data->i_dts ) - 1013370;
         /* This is another hack to prevent libavformat from triggering the
          * "non monotone timestamps" check in avformat/utils.c */

The above together with my skimming of the source makes me believe
that the behavior for negative timestamps are well-defined for the
muxers within *avformat* that might have issues with such, though if
there turns out to be problems that I have yet to encounter I would be
happy to polish the patch in some way or another.

Filip Roséen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20170207/4a35b4ff/attachment.html>

More information about the vlc-devel mailing list