title: Assigning of dts values to encoded packets
body: <p>I have a dump of H264-encoded data, which I need to put in mp4 container. I verified the validity of the encoded data by using mp4box utility against it. The mp4 file created by mp4box contained a proper 17 seconds long video. It is interesting that if I try ffmpeg to achieve the same, the resulting video is 34 seconds long and rather crappy (probably ffmpeg tries to decode video and then encode it, which results in the loss of video quality?) Anyway, for my project I can't use command line approach and need to come up wit a programmatic way to embed the data in the mp4 container.</p>
  
  <p>Below is the code I use (I removed error checking for brevity. During execution all the calls succeed):</p>
  
  <pre><code>AVFormatContext* pInputFormatContext = avformat_alloc_context();
  avformat_open_input(&amp;pInputFormatContext, "Data.264", NULL, NULL);
  avformat_find_stream_info(pInputFormatContext, NULL);
  AVRational* pTime_base = &amp;pInputFormatContext-&gt;streams[0]-&gt;time_base;
  
  int nFrameRate = pInputFormatContext-&gt;streams[0]-&gt;r_frame_rate.num / pFormatCtx-&gt;streams[0]-&gt;r_frame_rate.den;
  int nWidth = pInputFormatContext-&gt;streams[0]-&gt;codecpar-&gt;width;
  int nHeight = pInputFormatContext-&gt;streams[0]-&gt;codecpar-&gt;height;
  // nWidth = 1920, nHeight = 1080, nFrameRate = 25
  
  // Create output objects
  AVFormatContext* pOutputFormatContext = NULL;
  avformat_alloc_output_context2(&amp;pOutputFormatContext, NULL, NULL, "Destination.mp4");
  
  AVCodec* pVideoCodec = avcodec_find_encoder(pOutputFormatContext-&gt;oformat-&gt;video_codec/*AV_CODEC_ID_264*/);
  AVStream* pOutputStream = avformat_new_stream(pOutputFormatContext, NULL);
  pOutputStream-&gt;id = pOutputFormatContext-&gt;nb_streams - 1;
  AVCodecContext* pCodecContext = avcodec_alloc_context3(pVideoCodec);
  
  switch (pVideoCodec-&gt;type) {
  case AVMEDIA_TYPE_VIDEO:
    pCodecContext-&gt;codec_id = codec_id;
    pCodecContext-&gt;bit_rate = 400000;
    /* Resolution must be a multiple of two. */
    pCodecContext-&gt;width = nFrameWidth;
    pCodecContext-&gt;height = nFrameHeight;
    /* timebase: This is the fundamental unit of time (in seconds) in terms
    * of which frame timestamps are represented. For fixed-fps content,
    * timebase should be 1/framerate and timestamp increments should be
    * identical to 1. */
    pOutputStream-&gt;time_base.num = 1;
    pOutputStream-&gt;time_base.den = nFrameRate;
    pCodecContext-&gt;time_base = pOutputStream-&gt;time_base;
    pCodecContext-&gt;gop_size = 12; /* emit one intra frame every twelve frames at most */
    pCodecContext-&gt;pix_fmt = STREAM_PIX_FMT;
    break;
  default:
    break;
  }
  
  /* copy the stream parameters to the muxer */
  avcodec_parameters_from_context(pOutputStream-&gt;codecpar, pCodecContext);
  
  avio_open(&amp;pOutputFormatContext-&gt;pb, "Destination.mp4", AVIO_FLAG_WRITE);
  
  // Start writing
  AVDictionary* pDict = NULL;
  avformat_write_header(pOutputFormatContext, &amp;pDict);
  
  // Process packets
  AVPacket packet;
  int64_t nCurrentDts = 0;
  int64_t nDuration = 0;
  int nReadResult = 0;
  
  while (nReadResult == 0)
  {
    nReadResult = av_read_frame(m_pInputFormatContext, &amp;packet);
  // At this point, packet.dts == AV_NOPTS_VALUE. 
  // The duration field of the packet contains valid data
  
    packet.flags |= AV_PKT_FLAG_KEY;
    nDuration = packet.duration;
    packet.dts = nCurrentDts;
    packet.dts = av_rescale_q(nCurrentDts, pOutputFormatContext-&gt;streams[0]-&gt;codec-&gt;time_base, pOutputFormatContext-&gt;streams[0]-&gt;time_base);
    av_interleaved_write_frame(pOutputFormatContext, &amp;packet);
    nCurrentDts += nDuration;
    nDuration += packet.duration;
    av_free_packet(&amp;packet);
  }
  
  av_write_trailer(pOutputFormatContext);
  </code></pre>
  
  <p>The properties for the <em>Destination.mp4</em> file I receive indicate it is about 1 hour long with frame rate 0. I am sure the culprit is in the way I calculate dts values for each packet and use av_rescale_q(), but I do not have sufficient understanding of the avformat library to figure out the proper way to do it. Any help will be appreciated!</p>