title: encapsulating H.264 streams variable framerate in MPEG2 transport stream
body: <p>Imagine I have H.264 AnxB frames coming in from a real-time conversation. What is the best way to encapsulate in MPEG2 transport stream while maintaining the timing information for subsequent playback?</p>
  
  <p>I am using libavcodec and libavformat libraries. When I obtain pointer to object (*pcc) of type AVCodecContext, I set the foll.</p>
  
  <pre><code>pcc-&gt;codec_id = CODEC_ID_H264;
  pcc-&gt;bit_rate = br;
  pcc-&gt;width = 640;
  pcc-&gt;height = 480;
  pcc-&gt;time_base.num = 1;
  pcc-&gt;time_base.den = fps;
  </code></pre>
  
  <p>When I receive NAL units, I create a AVPacket and call av_interleaved_write_frame().</p>
  
  <pre><code>AVPacket pkt;
  av_init_packet( &amp;pkt );
  pkt.flags |= AV_PKT_FLAG_KEY;   
  pkt.stream_index = pst-&gt;index;
  pkt.data = (uint8_t*)p_NALunit;
  pkt.size = len;
  
  pkt.dts = AV_NOPTS_VALUE;
  pkt.pts = AV_NOPTS_VALUE;
  
  av_interleaved_write_frame( fc, &amp;pkt );
  </code></pre>
  
  <p>I basically have two questions: </p>
  
  <p>1) For variable framerate, is there a way to not specify the foll.
      pcc->time_base.num = 1;
      pcc->time_base.den = fps;
  and replace it with something to indicate variable framerate?</p>
  
  <p>2) While submitting packets, what "timestamps" should I assign to
  pkt.dts and pkt.pts?</p>
  
  <p>Right now, when I play the output using ffplay it is playing at constant framerate (fps) which I use in the above code.</p>
  
  <p>I also would love to know how to accommodate varying spatial resolution. In the stream that I receive, each keyframe is preceded by SPS and PPS. I know whenever the spatial resolution changes.
  IS there a way to not have to specify
      pcc->width = 640;
      pcc->height = 480;
  upfront? In other words, indicate that the spatial resolution can change mid-stream.</p>
  
  <p>Thanks a lot,
  Eddie</p>