I have a Samsung Galaxy S3 Mini (GT-I8190 @ 4.1.1) and I am unable to encode raw video frames into an h.264. I've tested on several other devices with different encoder vendors and I was able to get the working adding some vendor/version specific quirks.
None of the usual suspects worked, (aligning buffers... etc), I've found out on several custom ROMs with fixes for the odd input pixel format: example. That gave me some progress untill I was unable to configure the MediaCodec resulting on an assertion:
CHECK(def.nBufferSize >= size) failed.
I dug up some more and found this post with a fix for this problem. With that I was able to create and start the MediaCodec, but now i'm facing a buffer problem because the encoder only provides 12bytes for each input buffer, which is very strange.
Any hints on what is wrong?
Sample code:
MediaFormat format = MediaFormat.createVideoFormat("video/avc", width, height);
format.setInteger(MediaFormat.KEY_WIDTH, width);
format.setInteger(MediaFormat.KEY_HEIGHT, height);
format.setInteger(MediaFormat.KEY_BIT_RATE, bitrate);
format.setInteger(MediaFormat.KEY_FRAME_RATE, fps);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, capabilities.getH264EncoderPixelFormat());
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
format.setInteger("stride", stride);
format.setInteger("slice-height", sliceHeight);
format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);
codec = MediaCodec.createEncoderByType("video/avc");
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
codec.start();
ByteBuffer[] inputBuffers = codec.getInputBuffers();
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
Edit 1:
MediaFormat contents, just before the codec.configure(...):
{max-input-size=0, frame-rate=31, height=180, slice-height=192, color-format=2141192192, width=320, bitrate=64000, mime=video/avc, stride=320, i-frame-interval=1}
Edit 2:
The contents ByteBuffer[] inputBuffers after codec.getInputBuffers():
[java.nio.ReadWriteDirectByteBuffer, status: capacity=12 position=0 limit=12, java.nio.ReadWriteDirectByteBuffer, status: capacity=12 position=0 limit=12]