How to set encoder buffer size created by MediaCodec
Asked Answered
M

2

7

I am trying to use Nexus to test encoding with Mediacodec APIs. I can see the inputBuffers provided by the encoder is 119040 (by logging inputBuffers.capacity). But the size of the frame, i.e. input, is 460800. I got error message at inputBuffer.put with buffer overflow. So I was about to set the input buffer to 460800. The API I could find is BufferInfo.set. However, I cannot find a way to attach this setting to the encoder. Could someone help? Thanks!!!

encoder = MediaCodec.createByCodecName(codecInfo.getName());
ByteBuffer[] inputBuffers = encoder.getInputBuffers();
if (inputBufferIndex >= 0) {
    ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
    inputBuffer.clear();
    inputBuffer.put(input);
encoder.queueInputBuffer(inputBufferIndex, 0, input.length, 0, 0);}
Mesothelium answered 4/3, 2013 at 13:7 Comment(1)
Hello, Were you able to find the solution? If yes, please post some hints or the complete solution.Poesy
M
8

I'm late to the party, but based on: Android MediaCodec Documentation I think the correct way to change the buffer will be to adjust the MAX_INPUT_SIZE, something like:

int width=800;
int height=480;
encoder = MediaCodec.createByCodecName(codecInfo.getName());
format = MediaFormat.createVideoFormat ("video/avc", width, height);
format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE,655360); // 0.5MB but adjust it as you need.
Miru answered 11/1, 2017 at 0:9 Comment(0)
K
2

You don't set the size of the input buffer. The size is determined by the MediaFormat, specifically the width, height, and color format. If your input data has a different size, you will need to convert it to the format that the codec is expecting.

This isn't entirely trivial but is doable. For examples, see the buffer-to-buffer tests in the CTS EncodeDecodeTest. The test queries the codec to see what color format is supported, generates frames in that format, submits them to the encoder, then decodes the video and confirms that what comes out is the same as what went in.

The test generally requires API 18 (Android 4.3), but the buffer-to-buffer code will work in API 16. Whether or not it works on any given device is a different question -- since the CTS test didn't exist until API 18, it's possible for pre-4.3 devices to do this wrong.

Klemm answered 24/7, 2013 at 22:52 Comment(1)
Don't know how but when I am trying in Android 7, with YV12 image format as input and COLOR_FormatYUV420SemiPlanar as output, it gives me equal size of input image and input buffer. But in Android 9, where YV12 isn't supported, I had to use YUV_420_888 as input image format, and the size of input got increased. It isn't width * height * 3/2 anymore rather it has become width * height * 2. While the output size is same which is giving me buffer overflow problem. Can you suggest some hintsPoesy

© 2022 - 2024 — McMap. All rights reserved.