0

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]

Community
  • 1
  • 1
Agah PT
  • 130
  • 1
  • 9
  • 1
    Can you log the `MediaFormat` right before your call to `configure()`, and add the output to your question? Also, you may also want to read through http://stackoverflow.com/questions/21262797/converting-images-to-video/21266510#21266510 . – fadden Jan 27 '14 at 15:57
  • Been there, sill no improvements. I've added the info you requested. – Agah PT Jan 28 '14 at 09:32
  • 1
    I don't recognize color-format=2141192192 (0x7fa00000). Where did that value come from? What does it mean? What format is your input? – fadden Jan 28 '14 at 15:33
  • I get it from enumerating the codec `MediaCodecInfo.CodecCapabilities.colorFormats` and Found something related on [this](https://github.com/DroidModderXtreme/android_device_sony_kumquat-4.4/blob/master/patches/framework_native.patch) patch! – Agah PT Jan 30 '14 at 17:29

2 Answers2

0

Looks like you've selected a vendor-specific color format. According to the patch linked from the comments, 0x7FA00000 is OMX_STE_COLOR_FormatYUV420PackedSemiPlanarMB. Judging by the fact that it only gives you 12-byte buffers, I expect this is actually passing handles to the buffers rather than the buffer contents (similar to COLOR_FormatSurface).

Since you're trying to feed buffers of raw data in, you probably won't be able to get this to work unless you can reverse-engineer their allocator and their custom pixel formats (e.g. I see PIXEL_FORMAT_YCBCR42XMBN defined in that patch).

I'd recommend selecting the input format from a set of known formats. You can see an example in EncodeDecodeTest, in the selectColorFormat() method. For support across all devices with API >= 18, you need only handle two cases: planar and semi-planar YUV420.

fadden
  • 51,356
  • 5
  • 116
  • 166
  • I've dumped the color formats for this device: `OMX.ST.VFM.H264Enc: 2147483646, 2141192192, 2130708361` only '2130708361' maps to COLOR_FormatSurface, but i can't use it (api level). – Agah PT Jan 31 '14 at 11:59
  • That's not so good -- I was expecting to see 19 or 21. Is there another codec available (see `selectCodec()` in EncodeDecodedTest)? See also http://bigflake.com/mediacodec/#q5 re: color formats. You're running 4.1, so this device would not have had to pass the MediaCodec CTS tests, so there's no guarantee that this is supported. – fadden Jan 31 '14 at 15:36
0

After much battling this issue I was unable to resolve the problem, and it looks like there isn't a solution for this particular issue.

Agah PT
  • 130
  • 1
  • 9