[PATCH] videorec: Appropriately rescale timestamps and duration
authorVittorio Giovara <vittorio.giovara@gmail.com>
Tue, 10 Nov 2015 12:53:15 +0000 (13:53 +0100)
committerGianfranco Costamagna <locutusofborg@debian.org>
Thu, 12 Jan 2017 16:30:21 +0000 (16:30 +0000)
Applies to both audio and video for them to be in sync.

--HG--
extra : hg-git-rename-source : git

Gbp-Pq: Name 14a48ec6c0e4e7c4c475eacc9ae3b093656c163a.patch

hedgewars/avwrapper/avwrapper.c

index cc7d71245b32a30ae574069ecdcab89bc8915e48..f041af344b81bbad24cf48dda798c9ae744d6886 100644 (file)
@@ -66,6 +66,17 @@ static uint8_t g_OutBuffer[OUTBUFFER_SIZE];
 #if LIBAVCODEC_VERSION_MAJOR < 56
 #define av_frame_alloc                      avcodec_alloc_frame
 #define av_frame_free                       av_freep
+#define av_packet_rescale_ts                rescale_ts
+
+static void rescale_ts(AVPacket *pkt, AVRational ctb, AVRational stb)
+{
+    if (pkt->pts != AV_NOPTS_VALUE)
+        pkt->pts = av_rescale_q(pkt->pts, ctb, stb);
+    if (pkt->dts != AV_NOPTS_VALUE)
+        pkt->dts = av_rescale_q(pkt->dts, ctb, stb);
+    if (pkt->duration > 0)
+        pkt->duration = av_rescale_q(pkt->duration, ctb, stb);
+}
 #endif
 
 #if LIBAVCODEC_VERSION_MAJOR < 57
@@ -216,6 +227,8 @@ static int WriteAudioFrame()
         return FatalError("avcodec_encode_audio2 failed");
     if (!got_packet)
         return 0;
+
+    av_packet_rescale_ts(&Packet, g_pAudio->time_base, g_pAStream->time_base);
 #else
     if (NumSamples == 0)
         return 0;
@@ -374,10 +387,7 @@ static int WriteFrame(AVFrame* pFrame)
         if (!got_packet)
             return 0;
 
-        if (Packet.pts != AV_NOPTS_VALUE)
-            Packet.pts = av_rescale_q(Packet.pts, g_pVideo->time_base, g_pVStream->time_base);
-        if (Packet.dts != AV_NOPTS_VALUE)
-            Packet.dts = av_rescale_q(Packet.dts, g_pVideo->time_base, g_pVStream->time_base);
+        av_packet_rescale_ts(&Packet, g_pVideo->time_base, g_pVStream->time_base);
 #else
         Packet.size = avcodec_encode_video(g_pVideo, g_OutBuffer, OUTBUFFER_SIZE, pFrame);
         if (Packet.size < 0)