From 7c1ea99edf6fd6e67caaffc2ae602039134dac50 Mon Sep 17 00:00:00 2001 From: Vittorio Giovara Date: Tue, 10 Nov 2015 13:53:15 +0100 Subject: [PATCH] [PATCH] videorec: Appropriately rescale timestamps and duration 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 | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/hedgewars/avwrapper/avwrapper.c b/hedgewars/avwrapper/avwrapper.c index cc7d712..f041af3 100644 --- a/hedgewars/avwrapper/avwrapper.c +++ b/hedgewars/avwrapper/avwrapper.c @@ -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) -- 2.30.2