简体   繁体   English

MediaCodec H264编码器输出大量原始流

[英]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). 我在Android应用程序中使用MediaCodec对USB摄像机的帧进行编码,然后将该原始流馈送到mp4parser以创建mp4(API级别为16)。

Everything works really good on many devices, but i have issues with Galaxy S3 (I9300). 一切在许多设备上都可以正常运行,但是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. 问题是,当我将帧发送到编码器1分钟时,有时我会从编码器获得非常大的输出。

The size range is between 2.5MB-20MB. 大小范围在2.5MB-20MB之间。 This problem gets worse on longer clips, for example 7 minutes range is 9MB-120MB. 在较长的剪辑上,此问题会变得更糟,例如7分钟的范围是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? 是正确的,132代表什么?

Thank you for your help. 谢谢您的帮助。

The "132" means that the code was copied from the CTS test , which generates the video it encodes. “ 132”表示代码是从CTS测试中复制的, 该测试生成了它编码的视频。 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. 在880Kbps下,您期望(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. 我已经看到,当帧上的时间戳与帧速率不匹配时,这种情况会变得很糟糕(例如this post ),但是由于您正在生成时间戳,所以我不确定为什么会这样。

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). MediaCodec在Android 4.1(API 16)中有许多问题 ,在Android 4.3(API 18)中已解决。 What device and version of Android are you testing on? 您正在测试哪种设备和Android版本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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