AV Sync using Libavcodec
Date: Thu, 9 Aug 2007 13:06:29 +0100 From: "Charles Mason" <charlie.mas at gmail.com> Subject: [Ffmpeg-user] AV Sync using Libavcodec To: ffmpeg-user at mplayerhq.hu Message-ID: <1960fb140708090506w2b651888q4e07c17a7fe61c7 at mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1
I am currently writing an app to do video playback using libavformat and libavcodec. I have both audio and video streams decoding nicely. Now I want to sync the audio and video. As I can easily find out what position the audio is currently playing, I thought I could then drop / extend the video frame presentation to match.
Of course to be able to do that I need to know at what time each video frame should shown. I found a tutorial at the below address, which shows how to get av syncing to work. http://www.dranger.com/ffmpeg/tutorial05.html
In the tutorial it says not to trust most of the DTS and PTS variables and uses an extremely convoluted alternative. Is that tutorial still correct that most of the DTS and PTS values can not be relied upon in the latest versions of libavformat and libavcode? And is the method outlined in the tutorial the only viable way to get the correct time?
Charlie
What I was trying to get across was not that the PTS and DTS are not to be trusted. Instead, sometimes a frame may not have a proper PTS, and other times you may have to repeat a frame, so we have to adjust our information accordingly (this is what the synchronize_video() function is for). Then, when we display it, we have to figure out how long to delay before showing the next picture. So we see what the last gap in between PTSs was, and use that as our first guess. Then we compare the PTS of a given frame to where the audio is at. As you said, you can easily figure out where the audio is at, and we do this with our global audio_clock variable which is set in the audio_decode_frame() function. Then we make our delay larger or smaller based on that discrepancy. Then we compare the delay with the Actual Clock to make sure we don't drift. I don't think this is that convoluted, it's basically doing what you're saying: get the PTS and changing it based on what time it is and how the audio is being played. Maybe there might be a better way to do this, but I am basing this off of the ffplay source code. Check out ffplay.c for how they do it, too. --Stephen
On 8/14/07, Stephen Dranger <dranger at gmail.com> wrote:
I don't think this is that convoluted, it's basically doing what you're saying: get the PTS and changing it based on what time it is and how the audio is being played. Maybe there might be a better way to do this, but I am basing this off of the ffplay source code. Check out ffplay.c for how they do it, too.
I was trying to say the whole process of obtaining the PTS was convoluted, the actual syncing using it in your example seems pritty straight forward. When I first read your example and later ffplay, it seemed like it was a work around for a problem with FFMPEG. I realise now why the PTS can be missing from a frame. In fact your examples have been a massive help in getting my project working. FFMPEG whilst being a brilliant project is lacking a bit of up to date documentation and your examples were very useful in filling that gap. Thanks for your help Charlie
participants (2)
-
charlie.mas@gmail.com -
dranger@gmail.com