title: How to set average+max bitrate for MediaCodec to encode H.264 video?
body: <p>I'm using <code>MediaCodec</code> to encode H.264 video from camera, the problem is, when I move my phone, the output video's quality is very pool, full of mosaic/visual blocks in the video.</p>
  
  <p>Belows are some details:</p>
  
  <p>My encoder bitrate is 500 kbps, and bitrate-mode is <code>BITRATE_MODE_VBR</code>.</p>
  
  <p>I try to change bitrate to 800 kbps, the output video quality improves a lot, and if I change bitrate-mode into <code>BITRATE_MODE_CQ</code>, the output video quality is perfect, but the bitrate will increase up to 1400 kbps.</p>
  
  <p>I want to keep the average bitrate at a low level, e.g. 500 kbps, but allowing the bitrate burst into some bigger bitrate when the phone is moving, e.g. 800 kbps. In iOS VideoToolBox, there is an API <code>kVTCompressionPropertyKey_DataRateLimits</code> to set the average bitrate and the max bitrate, but in Android, how could I achieve that average-max bitrate control?</p>
  
  <p>I noticed that there is a hide "max-bitrate" parameter when configure the MediaCodec, but it has no effect (because Android framework ignore this parameter, see <a href="http://androidxref.com/7.1.1_r6/xref/frameworks/av/media/libstagefright/ACodec.cpp#configureCodec" rel="noreferrer">http://androidxref.com/7.1.1_r6/xref/frameworks/av/media/libstagefright/ACodec.cpp#configureCodec</a>), I also noticed that there is a hide “quality” parameter, but it also has no effect.</p>