From b9dd2068d6528d09d857e143e447cf76fb22a8cb Mon Sep 17 00:00:00 2001
From: Randrianasulu Andrew <randrianasulu@google.com>
Date: Thu, 5 May 2022 17:40:48 +0300
Subject: [PATCH 1/4] avformat/mpegtsenc: count packets instead of using
 avio_tell

---
 libavformat/mpegtsenc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index ead2ca2..a90258f 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -119,6 +119,7 @@ typedef struct MpegTSWrite {
     int64_t last_pat_ts;
     int64_t last_sdt_ts;
     int64_t last_nit_ts;
+    int64_t nb_packets;
 
     uint8_t provider_name[256];
 
@@ -946,7 +947,7 @@ invalid:
 
 static int64_t get_pcr(const MpegTSWrite *ts)
 {
-    return av_rescale(ts->total_size + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
+    return av_rescale(ts->nb_packets * TS_PACKET_SIZE + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
            ts->first_pcr;
 }
 
@@ -962,6 +963,7 @@ static void write_packet(AVFormatContext *s, const uint8_t *packet)
     }
     avio_write(s->pb, packet, TS_PACKET_SIZE);
     ts->total_size += TS_PACKET_SIZE;
+    ts->nb_packets++;
 }
 
 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
-- 
2.35.1

