[vlc-commits] avcodec: use macros to transform lavc timestamps to/from VLC timestamps

Steve Lhomme git at videolan.org
Wed Sep 19 11:58:35 CEST 2018


vlc | branch: master | Steve Lhomme <robux4 at ycbcr.xyz> | Fri May 11 08:43:48 2018 +0200| [0c8fe7c6b584cddbc7ed0dc87ac8a03b2740eb47] | committer: Steve Lhomme

avcodec: use macros to transform lavc timestamps to/from VLC timestamps

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=0c8fe7c6b584cddbc7ed0dc87ac8a03b2740eb47
---

 modules/codec/avcodec/encoder.c  | 16 ++++++++--------
 modules/codec/avcodec/subtitle.c |  4 ++--
 modules/codec/avcodec/video.c    | 16 ++++++++--------
 modules/demux/avformat/demux.c   | 19 +++++++++----------
 modules/demux/avformat/mux.c     |  8 ++++----
 modules/packetizer/avparser.c    |  2 +-
 6 files changed, 32 insertions(+), 33 deletions(-)

diff --git a/modules/codec/avcodec/encoder.c b/modules/codec/avcodec/encoder.c
index e64c3d5f8e..201049cf1f 100644
--- a/modules/codec/avcodec/encoder.c
+++ b/modules/codec/avcodec/encoder.c
@@ -1090,8 +1090,8 @@ static block_t *vlc_av_packet_Wrap(AVPacket *packet, vlc_tick_t i_length, AVCode
     b->packet = *packet;
 
     p_block->i_length = i_length;
-    p_block->i_pts = packet->pts;
-    p_block->i_dts = packet->dts;
+    p_block->i_pts = FROM_AV_TS(packet->pts);
+    p_block->i_dts = FROM_AV_TS(packet->dts);
     if( unlikely( packet->flags & AV_PKT_FLAG_CORRUPT ) )
         p_block->i_flags |= BLOCK_FLAG_CORRUPTED;
     if( packet->flags & AV_PKT_FLAG_KEY )
@@ -1106,7 +1106,7 @@ static void check_hurry_up( encoder_sys_t *p_sys, AVFrame *frame, encoder_t *p_e
 {
     vlc_tick_t current_date = vlc_tick_now();
 
-    if ( current_date + HURRY_UP_GUARD3 > frame->pts )
+    if ( current_date + HURRY_UP_GUARD3 > FROM_AV_TS(frame->pts) )
     {
         p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;
         p_sys->p_context->trellis = 0;
@@ -1116,7 +1116,7 @@ static void check_hurry_up( encoder_sys_t *p_sys, AVFrame *frame, encoder_t *p_e
     {
         p_sys->p_context->mb_decision = p_sys->i_hq;
 
-        if ( current_date + HURRY_UP_GUARD2 > frame->pts )
+        if ( current_date + HURRY_UP_GUARD2 > FROM_AV_TS(frame->pts) )
         {
             p_sys->p_context->trellis = 0;
             msg_Dbg( p_enc, "hurry up mode 2" );
@@ -1127,7 +1127,7 @@ static void check_hurry_up( encoder_sys_t *p_sys, AVFrame *frame, encoder_t *p_e
         }
     }
 
-    if ( current_date + HURRY_UP_GUARD1 > frame->pts )
+    if ( current_date + HURRY_UP_GUARD1 > FROM_AV_TS(frame->pts) )
     {
         frame->pict_type = AV_PICTURE_TYPE_P;
         /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */
@@ -1209,13 +1209,13 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
 
         if ( ( frame->pts != AV_NOPTS_VALUE ) && ( frame->pts != VLC_TICK_INVALID ) )
         {
-            if ( p_sys->i_last_pts == frame->pts )
+            if ( p_sys->i_last_pts == FROM_AV_TS(frame->pts) )
             {
                 msg_Warn( p_enc, "almost fed libavcodec with two frames with "
                           "the same PTS (%"PRId64 ")", frame->pts );
                 return NULL;
             }
-            else if ( p_sys->i_last_pts > frame->pts )
+            else if ( p_sys->i_last_pts > FROM_AV_TS(frame->pts) )
             {
                 msg_Warn( p_enc, "almost fed libavcodec with a frame in the "
                          "past (current: %"PRId64 ", last: %"PRId64")",
@@ -1223,7 +1223,7 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
                 return NULL;
             }
             else
-                p_sys->i_last_pts = frame->pts;
+                p_sys->i_last_pts = FROM_AV_TS(frame->pts);
         }
 
         frame->quality = p_sys->i_quality;
diff --git a/modules/codec/avcodec/subtitle.c b/modules/codec/avcodec/subtitle.c
index 9103440676..c2728588bd 100644
--- a/modules/codec/avcodec/subtitle.c
+++ b/modules/codec/avcodec/subtitle.c
@@ -220,7 +220,7 @@ static subpicture_t *DecodeBlock(decoder_t *dec, block_t **block_ptr)
     av_init_packet(&pkt);
     pkt.data = block->p_buffer;
     pkt.size = block->i_buffer;
-    pkt.pts  = block->i_pts;
+    pkt.pts  = TO_AV_TS(block->i_pts);
 
     int has_subtitle = 0;
     int used = avcodec_decode_subtitle2(sys->p_context,
@@ -243,7 +243,7 @@ static subpicture_t *DecodeBlock(decoder_t *dec, block_t **block_ptr)
     subpicture_t *spu = NULL;
     if (has_subtitle)
         spu = ConvertSubtitle(dec, &subtitle,
-                              subtitle.pts,
+                              FROM_AV_TS(subtitle.pts),
                               sys->p_context);
 
     /* */
diff --git a/modules/codec/avcodec/video.c b/modules/codec/avcodec/video.c
index ce64a9b0cc..560f8a5496 100644
--- a/modules/codec/avcodec/video.c
+++ b/modules/codec/avcodec/video.c
@@ -1059,8 +1059,8 @@ static int DecodeBlock( decoder_t *p_dec, block_t **pp_block )
             {
                 pkt.data = p_block->p_buffer;
                 pkt.size = p_block->i_buffer;
-                pkt.pts = p_block->i_pts != VLC_TICK_INVALID ? p_block->i_pts : AV_NOPTS_VALUE;
-                pkt.dts = p_block->i_dts != VLC_TICK_INVALID ? p_block->i_dts : AV_NOPTS_VALUE;
+                pkt.pts = p_block->i_pts != VLC_TICK_INVALID ? TO_AV_TS(p_block->i_pts) : AV_NOPTS_VALUE;
+                pkt.dts = p_block->i_dts != VLC_TICK_INVALID ? TO_AV_TS(p_block->i_dts) : AV_NOPTS_VALUE;
             }
             else
             {
@@ -1158,15 +1158,15 @@ static int DecodeBlock( decoder_t *p_dec, block_t **pp_block )
 
         /* Compute the PTS */
 #ifdef FF_API_PKT_PTS
-        vlc_tick_t i_pts = frame->pts;
-
-        if (i_pts == AV_NOPTS_VALUE )
-            i_pts = frame->pkt_dts;
+        int64_t av_pts = frame->pts == AV_NOPTS_VALUE ? frame->pkt_dts : frame->pts;
 #else
-        vlc_tick_t i_pts = frame->pkt_pts;
+        int64_t av_pts = frame->pkt_pts;
 #endif
-        if( i_pts == AV_NOPTS_VALUE )
+        vlc_tick_t i_pts;
+        if( av_pts == AV_NOPTS_VALUE )
             i_pts = date_Get( &p_sys->pts );
+        else
+            i_pts = FROM_AV_TS(av_pts);
 
         /* Interpolate the next PTS */
         if( i_pts != VLC_TICK_INVALID )
diff --git a/modules/demux/avformat/demux.c b/modules/demux/avformat/demux.c
index d59699e5d9..f4a84cb20b 100644
--- a/modules/demux/avformat/demux.c
+++ b/modules/demux/avformat/demux.c
@@ -675,7 +675,7 @@ int avformat_OpenDemux( vlc_object_t *p_this )
     }
 
     if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
-        i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
+        i_start_time = FROM_AV_TS(p_sys->ic->start_time);
 
     msg_Dbg( p_demux, "AVFormat(%s %s) supported stream", AVPROVIDER(LIBAVFORMAT), LIBAVFORMAT_IDENT );
     msg_Dbg( p_demux, "    - format = %s (%s)",
@@ -683,12 +683,12 @@ int avformat_OpenDemux( vlc_object_t *p_this )
     msg_Dbg( p_demux, "    - start time = %"PRId64, i_start_time );
     msg_Dbg( p_demux, "    - duration = %"PRId64,
              ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
-             p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
+             FROM_AV_TS(p_sys->ic->duration) : -1 );
 
     if( p_sys->ic->nb_chapters > 0 )
     {
         p_sys->p_title = vlc_input_title_New();
-        p_sys->p_title->i_length = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
+        p_sys->p_title->i_length = FROM_AV_TS(p_sys->ic->duration);
     }
 
     for( unsigned i = 0; i < p_sys->ic->nb_chapters; i++ )
@@ -815,7 +815,7 @@ static int Demux( demux_t *p_demux )
     if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
     {
         q = lldiv( p_sys->ic->start_time, AV_TIME_BASE);
-        i_start_time = q.quot * CLOCK_FREQ + q.rem * CLOCK_FREQ / AV_TIME_BASE;
+        i_start_time = q.quot * CLOCK_FREQ + FROM_AV_TS(q.rem);
     }
     else
         i_start_time = 0;
@@ -1002,7 +1002,7 @@ static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
 static int Control( demux_t *p_demux, int i_query, va_list args )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-    const int64_t i_start_time = p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ? p_sys->ic->start_time : 0;
+    const int64_t i_start_time = p_sys->ic->start_time != AV_NOPTS_VALUE ? p_sys->ic->start_time : 0;
     double f, *pf;
     int64_t i64, *pi64;
 
@@ -1063,7 +1063,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         case DEMUX_GET_LENGTH:
             pi64 = va_arg( args, int64_t * );
             if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
-                *pi64 = p_sys->ic->duration * CLOCK_FREQ / AV_TIME_BASE;
+                *pi64 = FROM_AV_TS(p_sys->ic->duration);
             else
                 *pi64 = 0;
             return VLC_SUCCESS;
@@ -1077,11 +1077,10 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         {
             i64 = va_arg( args, int64_t );
             bool precise = va_arg( args, int );
-            i64 = i64 * AV_TIME_BASE / CLOCK_FREQ + i_start_time;
 
             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
 
-            if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
+            if( av_seek_frame( p_sys->ic, -1, TO_AV_TS(i64) + i_start_time, AVSEEK_FLAG_BACKWARD ) < 0 )
             {
                 return VLC_EGENERIC;
             }
@@ -1193,8 +1192,8 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             if( !p_sys->p_title )
                 return VLC_EGENERIC;
 
-            i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *
-                  AV_TIME_BASE / CLOCK_FREQ + i_start_time;
+            i64 = TO_AV_TS(p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset) +
+                  i_start_time;
 
             msg_Warn( p_demux, "DEMUX_SET_SEEKPOINT: %"PRId64, i64 );
 
diff --git a/modules/demux/avformat/mux.c b/modules/demux/avformat/mux.c
index 0a900ce2a6..c2802497bd 100644
--- a/modules/demux/avformat/mux.c
+++ b/modules/demux/avformat/mux.c
@@ -361,11 +361,11 @@ static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
     }
 
     if( p_data->i_pts > 0 )
-        pkt.pts = p_data->i_pts * p_stream->time_base.den /
-            CLOCK_FREQ / p_stream->time_base.num;
+        pkt.pts = TO_AV_TS(p_data->i_pts * p_stream->time_base.den /
+            CLOCK_FREQ / p_stream->time_base.num);
     if( p_data->i_dts > 0 )
-        pkt.dts = p_data->i_dts * p_stream->time_base.den /
-            CLOCK_FREQ / p_stream->time_base.num;
+        pkt.dts = TO_AV_TS(p_data->i_dts * p_stream->time_base.den /
+            CLOCK_FREQ / p_stream->time_base.num);
 
     /* this is another hack to prevent libavformat from triggering the "non monotone timestamps" check in avformat/utils.c */
     p_stream->cur_dts = ( p_data->i_dts * p_stream->time_base.den /
diff --git a/modules/packetizer/avparser.c b/modules/packetizer/avparser.c
index 6e91375aa2..1f911bb328 100644
--- a/modules/packetizer/avparser.c
+++ b/modules/packetizer/avparser.c
@@ -179,7 +179,7 @@ static block_t *Packetize ( decoder_t *p_dec, block_t **pp_block )
 
     p_sys->i_offset += av_parser_parse2( p_sys->p_parser_ctx, p_sys->p_codec_ctx,
                                          &p_outdata, &i_outlen, p_indata, i_inlen,
-                                         p_block->i_pts, p_block->i_dts, -1);
+                                         TO_AV_TS(p_block->i_pts), TO_AV_TS(p_block->i_dts), -1);
 
     if( unlikely( i_outlen <= 0 || !p_outdata ) )
         goto out;



More information about the vlc-commits mailing list