title: PTS and DTS calculation for video and audio frames
body: <p>I am receiving video H264 encoded data and audio G.711 PCM encoded data from two different threads to mux / write into <code>mov</code> multimedia container.</p>
  
  <p>The writer function signatures are like:</p>
  
  <pre><code>bool WriteAudio(const unsigned char *pEncodedData, size_t iLength);
  bool WriteVideo(const unsigned char *pEncodedData, size_t iLength, bool const bIFrame);
  </code></pre>
  
  <p>And the function for adding audio and video streams looks like:</p>
  
  <pre><code>AVStream* AudioVideoRecorder::AddMediaStream(enum AVCodecID codecID) {
      Log("Adding stream: %s.", avcodec_get_name(codecID));
      AVCodecContext* pCodecCtx;
      AVStream* pStream;
  
      /* find the encoder */
      AVCodec* codec = avcodec_find_encoder(codecID);
      if (!codec) {
          LogErr("Could not find encoder for %s", avcodec_get_name(codecID));
          return NULL;
      }
  
      pStream = avformat_new_stream(m_pFormatCtx, codec);
      if (!pStream) {
          LogErr("Could not allocate stream.");
          return NULL;
      }
      pStream-&gt;id = m_pFormatCtx-&gt;nb_streams - 1;
      pStream-&gt;time_base = (AVRational){1, VIDEO_FRAME_RATE};
      pCodecCtx = pStream-&gt;codec;
  
  
      switch(codec-&gt;type) {
      case AVMEDIA_TYPE_VIDEO:
          pCodecCtx-&gt;codec_id = codecID;
          pCodecCtx-&gt;bit_rate = VIDEO_BIT_RATE;
          pCodecCtx-&gt;width = PICTURE_WIDTH;
          pCodecCtx-&gt;height = PICTURE_HEIGHT;
          pCodecCtx-&gt;gop_size = VIDEO_FRAME_RATE;
          pCodecCtx-&gt;pix_fmt = PIX_FMT_YUV420P;
          m_pVideoStream = pStream;
          break;
  
      case AVMEDIA_TYPE_AUDIO:
          pCodecCtx-&gt;codec_id = codecID;
          pCodecCtx-&gt;sample_fmt = AV_SAMPLE_FMT_S16;
          pCodecCtx-&gt;bit_rate = 64000;
          pCodecCtx-&gt;sample_rate = 8000;
          pCodecCtx-&gt;channels = 1;
          m_pAudioStream = pStream;
          break;
  
      default:
          break;
      }
  
      /* Some formats want stream headers to be separate. */
      if (m_pOutputFmt-&gt;flags &amp; AVFMT_GLOBALHEADER)
          m_pFormatCtx-&gt;flags |= CODEC_FLAG_GLOBAL_HEADER;
  
      return pStream;
  }
  </code></pre>
  
  <p>Inside <code>WriteAudio(..)</code> and <code>WriteVideo(..)</code> functions, I am creating <code>AVPakcet</code> using <code>av_init_packet(...)</code> and set <code>pEncodedData</code> and <code>iLength</code> as <code>packet.data</code> and <code>packet.size</code>. I printed <code>packet.pts</code> and <code>packet.dts</code> and its equivalent to <code>AV_NOPTS_VALUE</code>.</p>
  
  <p>Now, how do I calculate the PTS, DTS, and packet duration (<code>packet.dts</code>, <code>packet.pts</code> and <code>packet.duration</code>) correctly for both audio and video data so that I can sync audio &amp; video and play it properly? I saw many examples on the internet, but none of them are making sense to me. I am new with <code>ffmpeg</code>, and my conception may not be correct in some context. I want to do it in the appropriate way.</p>
  
  <p>Thanks in advance!</p>
  
  <p>EDIT: In my video streams, there is no B frame. So, I think PTS and DTS can be kept the same here.</p>