title: Android MediaCodec decode h264 raw frame
body: <p>I am using Android MediaCodec API to decode h264 frames. I could decode and render the frames on the view. My problem is the decoder miss lots of frames,especially the first some frames. <strong>DecodeMediaCodec.dequeueOutputBuffer()</strong> return -1.   aAbout 150 h264 frames,just decoded 43 frames. I can not find where the problem is. Here is my codes.</p>
  
  <pre><code> /**
   * init decoder
   */
  private void initDecodeMediaCodec()
  {
      mDecodeMediaCodec = MediaCodec.createDecoderByType(MIME_TYPE);
      MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE,
              VIDEO_WIDTH_640,
              VIDEO_HEIGHT_480);
  
      mDecodeMediaCodec.configure(format,
              new Surface(mRemoteVideoView.getSurfaceTexture()),
              null,
              0);
      mDecodeMediaCodec.start();
      mDecodeInputBuffers = mDecodeMediaCodec.getInputBuffers();
      System.out.println("decode-----"
              + mDecodeMediaCodec.getCodecInfo().getName());
  }
  </code></pre>
  
  <p>After decoder initial,I will start decoder thread.</p>
  
  <pre><code> /**
   * 
   * @param frameData
   */
  private void decode()
  {
      new Thread(new Runnable()
      {
          @Override
          public void run()
          {
              while (true)
              {
                  ByteBuffer decodeDataBuffer = null;
                  try
                  {
                      //take h264 frame from cache queue
                      decodeDataBuffer = decodeDataQuene.take();
                  }
                  catch (InterruptedException e)
                  {
                      e.printStackTrace();
                  }
  
  
                  BufferInfo info = new BufferInfo();
                  int inputBufferIndex = mDecodeMediaCodec.dequeueInputBuffer(-1);
                  System.out.println("inputBufferIndex: " + inputBufferIndex);
                  if (inputBufferIndex &gt;= 0)
                  {
                      ByteBuffer buffer = mDecodeInputBuffers[inputBufferIndex];
                      buffer.clear();
                      buffer.put(decodeDataBuffer.array());
                      mDecodeMediaCodec.queueInputBuffer(inputBufferIndex,
                              0,
                              decodeDataBuffer.array().length,
                              0,
                              0);
                      decodeDataBuffer.clear();
                      decodeDataBuffer = null;
                  }
  
                  int outputBufferIndex = mDecodeMediaCodec.dequeueOutputBuffer(info,
                         1000);
                  System.out.println("outputBufferIndex: "
                          + outputBufferIndex);
                  do
                  {
  
                      if (outputBufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER)
                      {
                          //no output available yet
                      }
                      else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED)
                      {
                          //encodeOutputBuffers = mDecodeMediaCodec.getOutputBuffers();
                      }
                      else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED)
                      {
                          MediaFormat formats = mDecodeMediaCodec.getOutputFormat();
                          //mediaformat changed
                      }
                      else if (outputBufferIndex &lt; 0)
                      {
                          //unexpected result from encoder.dequeueOutputBuffer
                      }
                      else
                      {
                          mDecodeMediaCodec.releaseOutputBuffer(outputBufferIndex,
                                  true);
  
                          outputBufferIndex = mDecodeMediaCodec.dequeueOutputBuffer(info,
                                  0);
                          System.out.println("inner outputBufferIndex: "
                                  + outputBufferIndex);
                      }
                  } while (outputBufferIndex &gt; 0);
              }
          }
      }).start();
  }
  </code></pre>
  
  <p>Anybody knows why?I hope your help.My android device is nexus 7.</p>