title: How to set pts and dts of AVPacket from RTP timestamps while muxing VP8 RTP stream to webm using ffmpeg libavformat?
body: <p>I am using ffmpeg libavformat library to write a video only webm file. I recieve VP8 encoded rtp stream on my server. I have successfully grouped the rtp byte stream (from rtp payload) into individual frames, and constructed a AVPacket. I am NOT re-encoding the payload to VP8 here as it is already vp8 encoded. </p>
  
  <p>I am writing the AVPacket to the file using av_write_interleaved() method. Though I am getting a webm file as output, it is not playing at all. When I checked for the info on the file using mkv tool's 'mkvinfo' command, I found the following info : </p>
  
  <pre><code>+ EBML head
  |+ EBML version: 1
  |+ EBML read version: 1
  |+ EBML maximum ID length: 4
  |+ EBML maximum size length: 8
  |+ Doc type: webm
  |+ Doc type version: 2
  |+ Doc type read version: 2
  + Segment, size 2142500
  |+ Seek head (subentries will be skipped)
  |+ EbmlVoid (size: 170)
  |+ Segment information
  | + Timestamp scale: 1000000
  | + Multiplexing application: Lavf58.0.100
  | + Writing application: Lavf58.0.100
  | + Duration: 78918744.480s (21921:52:24.480)
  |+ Segment tracks
  | + A track
  |  + Track number: 1 (track ID for mkvmerge &amp; mkvextract: 0)
  |  + Track UID: 1
  |  + Lacing flag: 0
  |  + Name: Video Track
  |  + Language: eng
  |  + Codec ID: V_VP8
  |  + Track type: video
  |  + Default duration: 1.000ms (1000.000 frames/fields per second for a 
  video track)
  |  + Video track
  |   + Pixel width: 640
  |   + Pixel height: 480
  |+ Tags
  | + Tag
  |  + Targets
  |  + Simple
  |   + Name: ENCODER
  |   + String: Lavf58.0.100
  | + Tag
  |  + Targets
  |   + TrackUID: 1
  |  + Simple
  |   + Name: DURATION
  |   + String: 21921:52:24.4800000
  |+ Cluster
  </code></pre>
  
  <p>As we can see, the duration of the stream is very disproportionately high. (My valid stream duration should be around 8-10 secs). And, the frame rate in the track info is also not what I am setting it to be. I am setting frame rate as 25 fps. </p>
  
  <p>I am applying av_scale_q(rtpTimeStamp, codec_timebase, stream_timebase) and setting the rescaled rtpTimeStamp as pts and dts values. My guess is my way of setting pts and dts is wrong. Please help me how to set pts and dts values on the AVPacket, so as get a working webm file with proper meta info on it. </p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>The following is the code I call to init the library :</p>
  
  <pre><code> #define STREAM_FRAME_RATE 25
   #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P 
  
   typedef struct OutputStream {
     AVStream *st;
     AVCodecContext *enc;
     AVFrame *frame;
   } OutputStream;
  
  
   typedef struct WebMWriter {
        OutputStream *audioStream, *videoStream;
        AVFormatContext *ctx;
        AVOutputFormat *outfmt;
        AVCodec *audioCodec, *videoCodec;
   } WebMWriter;
  
   static OutputStream audioStream = { 0 }, videoStream = { 0 };
  
   WebMWriter *init(char *filename)
   {
      av_register_all();
  
      AVFormatContext *ctx = NULL;
      AVCodec *audioCodec = NULL, *videoCodec = NULL;
      const char *fmt_name = NULL;
      const char *file_name = filename;
  
      int alloc_status = avformat_alloc_output_context2(&amp;ctx, NULL, fmt_name, file_name);
  
      if(!ctx)
              return NULL;
  
      AVOutputFormat *fmt = (*ctx).oformat;
  
      AVDictionary *video_opt = NULL;
      av_dict_set(&amp;video_opt, "language", "eng", 0);
      av_dict_set(&amp;video_opt, "title", "Video Track", 0);
  
      if(fmt-&gt;video_codec != AV_CODEC_ID_NONE)
      {
              addStream(&amp;videoStream, ctx, &amp;videoCodec, AV_CODEC_ID_VP8, video_opt);
      }
  
   if(videoStream.st)
              openVideo1(&amp;videoStream, videoCodec, NULL);
  
      av_dump_format(ctx, 0, file_name, 1);
  
      int ret = -1;
      /* open the output file, if needed */
      if (!(fmt-&gt;flags &amp; AVFMT_NOFILE)) {
              ret = avio_open(&amp;ctx-&gt;pb, file_name, AVIO_FLAG_WRITE);
              if (ret &lt; 0) {
                      printf("Could not open '%s': %s\n", file_name, av_err2str(ret));
                      return NULL;
              }
      }
  
      /* Write the stream header, if any. */
      AVDictionary *format_opt = NULL;
      ret = avformat_write_header(ctx, &amp;format_opt);
      if (ret &lt; 0) {
              fprintf(stderr, "Error occurred when opening output file: %s\n",
                              av_err2str(ret));
              return NULL;
      }
  
  
      WebMWriter *webmWriter = malloc(sizeof(struct WebMWriter));
      webmWriter-&gt;ctx = ctx;
      webmWriter-&gt;outfmt = fmt;
      webmWriter-&gt;audioStream = &amp;audioStream;
      webmWriter-&gt;videoStream = &amp;videoStream;
      webmWriter-&gt;videoCodec = videoCodec;
  
      return webmWriter;
   }
  </code></pre>
  
  <p>The following is the openVideo() method : </p>
  
  <pre><code> void openVideo1(OutputStream *out_st, AVCodec *codec, AVDictionary *opt_arg)
   {       
      AVCodecContext *codec_ctx = out_st-&gt;enc;
      int ret = -1;
      AVDictionary *opt = NULL;
      if(opt_arg != NULL)
      {       
              av_dict_copy(&amp;opt, opt_arg, 0);
              ret = avcodec_open2(codec_ctx, codec, &amp;opt);
      }
      else
      {       
              ret = avcodec_open2(codec_ctx, codec, NULL);
      }
  
      /* copy the stream parameters to the muxer */
      ret = avcodec_parameters_from_context(out_st-&gt;st-&gt;codecpar, codec_ctx);
      if (ret &lt; 0) {
              printf("Could not copy the stream parameters\n");
              exit(1);
      }
  
   }
  </code></pre>
  
  <p>The following is the addStream() method :</p>
  
  <pre><code> void addStream(OutputStream *out_st, AVFormatContext *ctx, AVCodec **cdc, enum AVCodecID codecId, AVDictionary *opt_arg)
   {
  
      (*cdc) = avcodec_find_encoder(codecId);
      if(!(*cdc)) {
              exit(1);
      }
  
      /*as we are passing a NULL AVCodec cdc, So AVCodecContext codec_ctx will not be allocated, we have to do it explicitly */
      AVStream *st = avformat_new_stream(ctx, *cdc);
      if(!st) {
              exit(1);
      }
  
      out_st-&gt;st = st;
      st-&gt;id = ctx-&gt;nb_streams-1;
  
      AVDictionary *opt = NULL;
      av_dict_copy(&amp;opt, opt_arg, 0);
      st-&gt;metadata = opt;
  
      AVCodecContext *codec_ctx = st-&gt;codec;
      if (!codec_ctx) {
              fprintf(stderr, "Could not alloc an encoding context\n");
              exit(1);
      }
      out_st-&gt;enc = codec_ctx;
  
      codec_ctx-&gt;strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  
   switch ((*cdc)-&gt;type) {
              case AVMEDIA_TYPE_AUDIO:
                      codec_ctx-&gt;codec_id = codecId;
                      codec_ctx-&gt;sample_fmt  = AV_SAMPLE_FMT_FLTP;
                      codec_ctx-&gt;bit_rate    = 64000;
                      codec_ctx-&gt;sample_rate = 48000;
                      codec_ctx-&gt;channels    = 2;//1;
                      codec_ctx-&gt;channel_layout = AV_CH_LAYOUT_STEREO; 
                      codec_ctx-&gt;codec_type = AVMEDIA_TYPE_AUDIO;
                      codec_ctx-&gt;time_base = (AVRational){1,STREAM_FRAME_RATE};
  
  
                      break;
  
              case AVMEDIA_TYPE_VIDEO:
                      codec_ctx-&gt;codec_id = codecId;
                      codec_ctx-&gt;bit_rate = 90000;
                      codec_ctx-&gt;width    = 640;
                      codec_ctx-&gt;height   = 480;
  
  
                      codec_ctx-&gt;time_base = (AVRational){1,STREAM_FRAME_RATE};
                      codec_ctx-&gt;gop_size = 12;
                      codec_ctx-&gt;pix_fmt = STREAM_PIX_FMT;
                      codec_ctx-&gt;codec_type = AVMEDIA_TYPE_VIDEO;
  
                      break;
  
              default:
                      break;
      }
  
   /* Some formats want stream headers to be separate. */
      if (ctx-&gt;oformat-&gt;flags &amp; AVFMT_GLOBALHEADER)
              codec_ctx-&gt;flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
   }
  </code></pre>
  
  <p>The following is the code I call to write a frame of data to the file :</p>
  
  <pre><code> int writeVideoStream(AVFormatContext *ctx, AVStream *st, uint8_t *data, int size, long frameTimeStamp, int isKeyFrame, AVCodecContext *codec_ctx)
   {       
      AVRational rat = st-&gt;time_base;
      AVPacket pkt = {0};
      av_init_packet(&amp;pkt);
  
      void *opaque = NULL;
      int flags = AV_BUFFER_FLAG_READONLY;
      AVBufferRef *bufferRef = av_buffer_create(data, size, NULL, opaque, flags);
  
      pkt.buf = bufferRef;
      pkt.data = data;
      pkt.size = size;  
      pkt.stream_index  = st-&gt;index;
  
      pkt.pts = pkt.dts = frameTimeStamp;
      pkt.pts = av_rescale_q(pkt.pts, codec_ctx-&gt;time_base, st-&gt;time_base);
      pkt.dts = av_rescale_q(pkt.dts, codec_ctx-&gt;time_base, st-&gt;time_base);
  
  
      if(isKeyFrame == 1)
              pkt.flags |= AV_PKT_FLAG_KEY;
  
      int ret = av_interleaved_write_frame(ctx, &amp;pkt);
      return ret;
   }
  </code></pre>
  
  <p><strong>NOTE :</strong>
  Here 'frameTimeStamp' is the rtp timeStamp on the rtp packet of that frame.</p>
  
  <p><strong>EDIT 2.0 :</strong></p>
  
  <p>My updated addStream() method with codecpars changes :</p>
  
  <pre><code> void addStream(OutputStream *out_st, AVFormatContext *ctx, AVCodec **cdc, enum AVCodecID codecId, AVDictionary *opt_arg)
   {
  
      (*cdc) = avcodec_find_encoder(codecId);
      if(!(*cdc)) {
              printf("@@@@@ couldnt find codec \n");
              exit(1);
      }
  
      AVStream *st = avformat_new_stream(ctx, *cdc);
      if(!st) {
              printf("@@@@@ couldnt init stream\n");
              exit(1);
      }
  
      out_st-&gt;st = st;
      st-&gt;id = ctx-&gt;nb_streams-1;
      AVCodecParameters *codecpars = st-&gt;codecpar;
      codecpars-&gt;codec_id = codecId;
      codecpars-&gt;codec_type = (*cdc)-&gt;type;
  
      AVDictionary *opt = NULL;
      av_dict_copy(&amp;opt, opt_arg, 0);
      st-&gt;metadata = opt;
      //av_dict_free(&amp;opt);
  
      AVCodecContext *codec_ctx = st-&gt;codec;
      if (!codec_ctx) {
              fprintf(stderr, "Could not alloc an encoding context\n");
              exit(1);
      }
      out_st-&gt;enc = codec_ctx;
  
      //since opus is experimental codec
      //codec_ctx-&gt;strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  
   switch ((*cdc)-&gt;type) {
              case AVMEDIA_TYPE_AUDIO:
                      codec_ctx-&gt;codec_id = codecId;
                      codec_ctx-&gt;sample_fmt  = AV_SAMPLE_FMT_FLTP;//AV_SAMPLE_FMT_U8 or AV_SAMPLE_FMT_S16;
                      codec_ctx-&gt;bit_rate    = 64000;
                      codec_ctx-&gt;sample_rate = 48000;
                      codec_ctx-&gt;channels    = 2;//1;
                      codec_ctx-&gt;channel_layout = AV_CH_LAYOUT_STEREO; //AV_CH_LAYOUT_MONO;
                      codec_ctx-&gt;codec_type = AVMEDIA_TYPE_AUDIO;
                      codec_ctx-&gt;time_base = (AVRational){1,STREAM_FRAME_RATE};
  
                      codecpars-&gt;format = codec_ctx-&gt;sample_fmt;
                      codecpars-&gt;channels = codec_ctx-&gt;channels;
                      codecpars-&gt;sample_rate = codec_ctx-&gt;sample_rate;
  
                      break;
  
              case AVMEDIA_TYPE_VIDEO:
                      codec_ctx-&gt;codec_id = codecId;
                      codec_ctx-&gt;bit_rate = 90000;
                      codec_ctx-&gt;width    = 640;
                      codec_ctx-&gt;height   = 480;
  
                      codec_ctx-&gt;time_base = (AVRational){1,STREAM_FRAME_RATE};
                      codec_ctx-&gt;gop_size = 12;
                      codec_ctx-&gt;pix_fmt = STREAM_PIX_FMT;
                      //codec_ctx-&gt;max_b_frames = 1;
                      codec_ctx-&gt;codec_type = AVMEDIA_TYPE_VIDEO;
                      codec_ctx-&gt;framerate = av_inv_q(codec_ctx-&gt;time_base);
                      st-&gt;avg_frame_rate = codec_ctx-&gt;framerate;//(AVRational){25000, 1000};
  
                      codecpars-&gt;format = codec_ctx-&gt;pix_fmt;
                      codecpars-&gt;width = codec_ctx-&gt;width;
                      codecpars-&gt;height = codec_ctx-&gt;height;
                      codecpars-&gt;sample_aspect_ratio = (AVRational){codec_ctx-&gt;width, codec_ctx-&gt;height};
  
                      break;
  
              default:
                      break;
      }      
      codecpars-&gt;bit_rate = codec_ctx-&gt;bit_rate;
  
      int ret = avcodec_parameters_to_context(codec_ctx, codecpars);
      if (ret &lt; 0) {
              printf("Could not copy the stream parameters\n");
              exit(1);
      }
  
      /* Some formats want stream headers to be separate. */
      if (ctx-&gt;oformat-&gt;flags &amp; AVFMT_GLOBALHEADER)
              codec_ctx-&gt;flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
   }
  </code></pre>