title: Inconsistent sample time / presentation time during video decoding
body: <p>I'm writing an APP that can encode video by camera input and process video by decode-edit-encode steps. For the camera, I use the Camera class rather than Intent to configure the details settings of the camera. Then I feed the camera frames to the encoder (MediaCodec in API 16) and the muxer (I use ffmpeg muxer since I want to work on 4.1 Devices).</p>
  
  <p>I measure the time code of camera frames by system nano time, and select a subset of frames to fit a desired FPS (currently 15). There are some small "noises" in the time values, for example (in ms): 0, 60718, 135246, 201049, ... rather than 0, 66000, 133000, 200000, ... .</p>
  
  <p>After some trying of configuring the muxer correctly (As <a href="https://stackoverflow.com/questions/22324990/problems-of-using-mediacodec-getoutputformat-for-an-encoder-in-android-4-1-4-2">this question</a>), I can produce a video (with AVC codec) that can be playback by the video player on devices. The playback speed is correct so I think the video should have correct time information of the frames.</p>
  
  <p>However, I got a problem when I try to decode the video to perform the video editting process. I use the standard video extract/decode steps as <a href="http://bigflake.com/mediacodec/" rel="nofollow noreferrer">these samples</a>, like this:</p>
  
  <pre><code>int decode_input_index = decoder.dequeueInputBuffer(TIMEOUT_USEC);
  if (decode_input_index &gt;= 0)
  {
      ByteBuffer decoder_input_buffer = decode_input_buffers[decode_input_index];
      int sample_size = extractor.readSampleData(decoder_input_buffer, 0);
      if (sample_size &lt; 0)
      {
          decoder.queueInputBuffer(decode_input_index, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
          is_decode_input_done = true;
      }
      else
      {
          long sample_time = extractor.getSampleTime();
          decoder.queueInputBuffer(decode_input_index, 0, sample_size, sample_time, 0);
          extractor.advance();
      }
  }
  else
  {
      Log.v(TAG, "Decoder dequeueInputBuffer timed out! Try again later");
  }
  </code></pre>
  
  <p>The sample time from getSampleTime() has correct value as I encode the video. (e.g., they are exactly 0, 60718, 135246, 201049, ... in us). It is also the presentation time in the input of decoder.queueInputBuffer(). When the decoder proceeds to decode this frame, I get the frame time by:</p>
  
  <pre><code>int decode_output_index = decoder.dequeueOutputBuffer(decode_buffer_info, TIMEOUT_USEC);
  switch (decode_output_index)
  {
      ....
      (some negative-value flags in MediaCodec)
      ....
      default:
      {
          ByteBuffer decode_output_buffer = decode_output_buffers[decode_output_index];
          long ptime_us = decode_buffer_info.presentationTimeUs;
          boolean is_decode_EOS = ((decode_buffer_info.flags &amp; MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0);
  
          ....
      }
  }
  </code></pre>
  
  <p>I expect to set the same time sequence as the one in decoder input, but I get a lot of 0's from the BufferInfo at decoder output. The decoded frame contents seems to be correct, but most of the presentation time values are 0. Only the last few frames has correct presentation time.</p>
  
  <p>I test the whole same process on a device with Android 4.3 (even with the same ffmpeg muxer rather than MediaMuxer in API 18), and everything looks fine. On 4.1/4.2 devices, if I capture the video by the built-in camera APP on the device and then decode the video, then the presentation time is also correct although the time values also have noises due to camera delay.</p>
  
  <p>What's wrong with the video or the decode process, when the video can be playbacked and decoded normally, but with correct sample time and bad presentation time? I may have to use a workaround to measure the presentation time by the sample time (It's easy by using a queue), but I want to figure out if there is any missing part in my work.</p>