简体   繁体   中英

MediaCodec h264 encoder outputs large raw stream

I'm using MediaCodec in an Android app to encode frames from a Usb camera and then feed that raw stream to mp4parser to create mp4(API level is 16).

Everything works really good on many devices, but i have issues with Galaxy S3 (I9300).

The problem is that when i send frames to the encoder for 1 minute, sometimes i get a very large output from the encoder.

The size range is between 2.5MB-20MB. This problem gets worse on longer clips, for example 7 minutes range is 9MB-120MB.

Is that normal?

I tried to capture the same scene, but still got different results.

The encoder settings:

Codec: OMX.SEC.AVC.Encoder
Color Format: 21
KEY_FRAME_RATE: 8.77
KEY_BIT_RATE: ~880Kbit

One more thing, when sending the frames to the encoder i'm using:

mediaVideoCodec.queueInputBuffer(inputBufferIndex, 0, FrameData.length, computePresentationTime(frameCounter), 0);

and the function

private long computePresentationTime(int frameIndex) {
    return (long)(132 + (frameIndex * (1000000f / 8.77f)));
}

The encoding is done in a background thread, so i'm not sure i can use system time here, unless the producer saves the frame and time. Is that correct and what does 132 represents?

Thank you for your help.

The "132" means that the code was copied from the CTS test , which generates the video it encodes. The test code is creating a series of frames with a constant frame rate. As part of an effort to determine that the timestamps are being passed through unmodified, and not generated internally by the codec, a small offset is added.

At 880Kbps, you'd expect (880/8)*(7*60)/1024 = 45MB. Most encoders do a pretty good job of matching the desired rate. I've seen this go bad when the timestamps on the frames don't match the frame rate (eg this post ), but since you're generating the time stamps I'm not sure why that would be.

Ideally the timestamp comes from the camera, not a generated value or the system time. Otherwise you have no way of knowing if a frame was dropped near the source.

MediaCodec has many problems in Android 4.1 (API 16) that were resolved in Android 4.3 (API 18). What device and version of Android are you testing on?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM