[FFmpeg-devel] [PATCH] fix rounding errors in .ass subtitles format timings

Muhammed mohammedkaabi64 at gmail.com
Tue Nov 21 15:38:08 EET 2023


Signed-off-by: mohad12211 <mohammedkaabi64 at gmail.com>
---
  fftools/ffmpeg_mux.c  | 2 ++
  libavcodec/avpacket.c | 9 +++++++++
  libavcodec/packet.h   | 2 ++
  3 files changed, 13 insertions(+)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 30c0330..7c9f03c 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -92,6 +92,8 @@ static int write_packet(Muxer *mux, OutputStream *ost, 
AVPacket *pkt)
          pkt->pts = pkt->dts;
           pkt->duration = av_rescale_q(pkt->duration, pkt->time_base, 
ost->st->time_base);
+    } else if (ost->type == AVMEDIA_TYPE_SUBTITLE && 
ost->par_in->codec_id == AV_CODEC_ID_ASS) {
+        av_packet_rescale_ts_end(pkt, pkt->time_base, ost->st->time_base);
      } else
          av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
      pkt->time_base = ost->st->time_base;
diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index e29725c..3926590 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -524,6 +524,15 @@ int av_packet_make_writable(AVPacket *pkt)
      return 0;
  }
  +void av_packet_rescale_ts_end(AVPacket *pkt, AVRational src_tb, 
AVRational dst_tb)
+{
+    int64_t old_pts = pkt->pts;
+    pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
+    if (pkt->dts != AV_NOPTS_VALUE)
+        pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
+    pkt->duration = av_rescale_q(pkt->duration + old_pts, src_tb, 
dst_tb) - pkt->pts;
+}
+
  void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational 
dst_tb)
  {
      if (pkt->pts != AV_NOPTS_VALUE)
diff --git a/libavcodec/packet.h b/libavcodec/packet.h
index b19409b..49be5cd 100644
--- a/libavcodec/packet.h
+++ b/libavcodec/packet.h
@@ -839,6 +839,8 @@ int av_packet_make_writable(AVPacket *pkt);
   */
  void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational 
tb_dst);
  +void av_packet_rescale_ts_end(AVPacket *pkt, AVRational tb_src, 
AVRational tb_dst);
+
  /**
   * @}
   */
-- 
2.42.1

My proposed fix for https://trac.ffmpeg.org/ticket/10659



More information about the ffmpeg-devel mailing list