title: How to synchronize audio and video using ffmpeg libraries?
body: <p>Stuck writing a very basic media player in C, using SDL and ffmpeg libraries. Initially, followed the theory in <a href="http://dranger.com/ffmpeg" rel="nofollow noreferrer">this</a> page to get an idea about the entire program and the usage of libraries. After coding from scratch, thanks to that tutorial and many other resources,  finally I made my code work, using the latest libraries of ffmpeg and SDL (2.0). But my code lacks a proper synchronization mechanism (actually it lacks a sync mechanism !).  </p>
  
  <p>I still don't have a clear idea on how to synchronize the audio and video together as the theory provided in the link is only <em>very partially</em> correct (atleast when it comes to using the latest dev libraries).<br>
  For example, a sentence in <a href="http://dranger.com/ffmpeg/tutorial05.html" rel="nofollow noreferrer">this</a> page is as follows : </p>
  
  <blockquote>
    <p>However, ffmpeg reorders the packets so that the DTS of the packet being processed by avcodec_decode_video() will always be the same as the PTS of the frame it returns.  </p>
  </blockquote>
  
  <p>I am using <em>avcodec_decode_video2()</em> and the DTS of the packet is definitely not the same as pts of the frame it decodes (in general).</p>
  
  <p>I read <a href="http://downloads.bbc.co.uk/rd/pubs/reports/1996-02.pdf" rel="nofollow noreferrer">this</a> very informative BBC report and it makes complete sense. I have a clear idea about PTS and DTS. But the PTS and DTS values that ffmpeg is using for packets and decoded frames is confusing. I wish there were some documentation on that aspect.  </p>
  
  <p>Can someone explain the steps to synchronize audio and video ? I only need the steps. I am quite comfortable implementing them. Any help is greatly appreciated. Thanks !  </p>
  
  <p>PS : Here's a screenshot of what I am talking about :  </p>
  
  <p><img src="https://i.stack.imgur.com/30ANV.png" alt="enter image description here">  </p>
  
  <p>The huge negative value is, I assume AV_NOPTS_VALUE.</p>