title: How to configure the encoder input buffer size created by MediaCodec?
body: <p>I'm working to decode a video file and then encode to a smaller size/bit rate video file. I have finished the process of decoding and getting the raw video output buffer,but when I queue the raw output buffer to the input buffer of the encoder, it throws an overflow exception. As the capacity of the input buffer is too small to hold the raw output buffer.</p>
  
  <p>I find that if I configure the width and height of the output format of the encoder bigger, the capacity of both the input and output buffer of the encoder will be bigger too.And they are very near in values.And when I configure the width and height as the orignal video size,the input buffer is big enough to hold the raw output buffer of the decoder, and I get the output video file.But I want to get a smaller size and smaller bit size video.</p>
  
  <p>The key code is as belows.</p>
  
  <pre><code>MediaCodecInfo codecInfo = selectCodec("video/avc");
  
  MediaFormat outformat = MediaFormat.createVideoFormat("video/avc", 1280, 720);
  
  int colorfmt = selectColorFormat(codecInfo, "video/avc");
  
  outformat.setInteger(MediaFormat.KEY_COLOR_FORMAT, colorfmt);//2141391876);    
  
  outformat.setInteger(MediaFormat.KEY_BIT_RATE, 178*1024*8);
  
  outformat.setInteger(MediaFormat.KEY_FRAME_RATE, 24);
  
  outformat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
  
  //outformat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 3400000);;
  
  MediaCodec encoder = MediaCodec.createEncoderByType("video/avc");
  
  encoder.configure(outformat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
  
  encoder.start();
  </code></pre>
  
  <p>Extract video and decode</p>
  
  <pre><code>   int inIndex = decoder.dequeueInputBuffer(10000);
  
     if (inIndex &gt;= 0) {
  
     ByteBuffer buffer = inputBuffers[inIndex];
  
     int sampleSize = extractor.readSampleData(buffer, 0);
  
     if (sampleSize &lt; 0) {
  
         decoder.queueInputBuffer(inIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
  
         isEOS = true;
  
     } else {
  
         decoder.queueInputBuffer(inIndex, 0, sampleSize, extractor.getSampleTime(), 0);
  
      extractor.advance();
  
     }
  </code></pre>
  
  <p>}</p>
  
  <p>Encode and mux to mp4 file</p>
  
  <pre><code>int outIndex = decoder.dequeueOutputBuffer(info, 10000);
  
  switch (outIndex) {
  
  case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
  
      Log.d("DecodeActivity", "INFO_OUTPUT_BUFFERS_CHANGED");
  
      outputBuffers = decoder.getOutputBuffers();
  
      break;
  
  case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
  
      Log.d("DecodeActivity", "New format " + decoder.getOutputFormat());
  
      MediaFormat infmt = decoder.getOutputFormat();
  
      break;
  
  case MediaCodec.INFO_TRY_AGAIN_LATER:
  
      Log.d("DecodeActivity", "dequeueOutputBuffer timed out!");
  
      break;
  
  default:
  
      ByteBuffer buffer = outputBuffers[outIndex];
      buffer.position(info.offset);
      buffer.limit(info.offset + info.size);
      int encInputIndex = encoder.dequeueInputBuffer(10000);
      if (encInputIndex &gt;= 0) {
          ByteBuffer encBuffer = encInputBuf[encInputIndex];
          encBuffer.clear();
          encBuffer.put(buffer);
          encoder.queueInputBuffer(encInputIndex, 0, info.size, info.presentationTimeUs,0);
      }
  
  
  
  ByteBuffer[] encOutputBuf = encoder.getOutputBuffers();
     int trackindex = 0;
     while(true) {
         MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
         int encoderStatus = encoder.dequeueOutputBuffer(bufferInfo, 10000);
         if (encoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
            // no output available yet
            break;                
         } else if (encoderStatus == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
            // not expected for an encoder
            encOutputBuf = encoder.getOutputBuffers();
         } else if (encoderStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
            MediaFormat newFormat = encoder.getOutputFormat();
            // now that we have the Magic Goodies, start the muxer
            trackindex = muxer.addTrack(newFormat);
            muxer.start();
       } else if (encoderStatus &lt; 0) {
            // let's ignore it
       } else {
            ByteBuffer buf = encOutputBuf[encoderStatus];
            muxer.writeSampleData(trackindex, buf, bufferInfo);
            encoder.releaseOutputBuffer(encoderStatus, false);
       }
  }
  </code></pre>
  
  <p>@fadden Wish to discuss with you and get your help.Thanks!</p>