title: FFMPEG library- transcode raw image to h264 stream, and the output file does not contains pts and dts info
body: <p>I am trying using ffmpeg c++ library to convert several raw yuyv image to h264 stream, the image come from memory and passed as string about 24fps, i do the convention as the following steps:</p>
  <ol>
  <li>init AVFormatContext,AVCodec,AVCodecContext and create new AVStream. this step i mainly refer to <a href="https://github.com/leandromoreira/ffmpeg-libav-tutorial/blob/master/3_transcoding.c" rel="nofollow noreferrer">ffmpeg-libav-tutorial</a>, and AVFormatContext use customize <code>write_buffer()</code> function(refer to <a href="https://github.com/leixiaohua1020/simplest_ffmpeg_mem_handler" rel="nofollow noreferrer">simplest_ffmpeg_mem_handler</a>)</li>
  <li>receive raw frame data, set width and height(1920x1080), and set pts and dts. here i manually set the output fps to 24, and use a global counter to count num of frames, and the pts is calculated by this counter, code snippet(<code>video_avs</code> is AVStream,<code>output_fps</code> is 24 and <code>time_base</code> is 1/24):</li>
  </ol>
  <pre><code>    input_frame-&gt;width = w;  // 1920
      input_frame-&gt;height = h;  // 1080
      input_frame-&gt;pkt_dts = input_frame-&gt;pts = global_pts;
      global_pts += video_avs-&gt;time_base.den/video_avs-&gt;time_base.num / output_fps.num * output_fps.den;
  </code></pre>
  <ol start="3">
  <li><p>convert it from yuyv to yuv422(because h264 does not support yuyv) and resize it from 1920x1080 to 640x480(because i need this resolution output), use <code>sws_scale()</code></p>
  </li>
  <li><p>use <code>avcodec_send_frame()</code> and <code>avcodec_receive_packet()</code> to get the output packet. set  <code>output_packet</code> duration and stream_index, then use <code>av_write_frame()</code> to write frame data.</p>
  </li>
  </ol>
  <pre><code>    AVPacket *output_packet = av_packet_alloc();
      int response = avcodec_send_frame(encoder-&gt;video_avcc, frame);
      while (response &gt;= 0) {
          response = avcodec_receive_packet(encoder-&gt;video_avcc, output_packet); // !! here output_packet.size is calculated
          if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
              break;
          } 
          else if (response &lt; 0) {
              printf(&quot;Error while sending packet to decoder&quot;);  // ??av_err2str(response)会报错
              return response;
          }
  
          // duration = next_pts - this_pts = timescale / fps = 1 / timebase / fps
          output_packet-&gt;duration = (encoder-&gt;video_avs-&gt;time_base.den / encoder-&gt;video_avs-&gt;time_base.num) / (output_fps.num / output_fps.den);
          output_packet-&gt;stream_index = 0;
          int response = av_write_frame(encoder-&gt;avfc, output_packet);  // packet order are not ensure
          if (response != 0) { printf(&quot;Error %d while receiving packet from decoder&quot;, response); return -1;}   
      }
      av_packet_unref(output_packet);
      av_packet_free(&amp;output_packet);
  </code></pre>
  <ol start="5">
  <li>in <code>write_buffer()</code> function, video stream output is stored to string variable, and then i write this string to file with ostream, and suffix mp4.</li>
  </ol>
  <p>after all the above steps, the output.mp4 cannot be played, the <code>ffprobe output.mp4 -show_frames</code> output is
  (<a href="https://i.stack.imgur.com/AgcLK.png" rel="nofollow noreferrer">image</a>):</p>
  <pre><code>Input #0, h264, from '/Users/ming/code/dev/haomo/output.mp4':
    Duration: N/A, bitrate: N/A
      Stream #0:0: Video: h264 (High 4:2:2), yuv422p(progressive), 640x480, 24.92 fps, 24 tbr, 1200k tbn, 48 tbc
  [FRAME]
  media_type=video
  stream_index=0
  key_frame=1
  pkt_pts=N/A
  pkt_pts_time=N/A
  pkt_dts=N/A
  pkt_dts_time=N/A
  best_effort_timestamp=N/A
  best_effort_timestamp_time=N/A
  </code></pre>
  <p>Note that before and after calling <code>av_write_frame()</code> in step 4, the passed argument <code>output_packet</code> contains correct pts and dts info, i cannot figure out why the output stream lost these info.</p>