简体   繁体   English

Camera2 OpenGL 由 Matrix.rotateM 引起的视频随机故障

[英]Camera2 OpenGL random glitches in video caused by Matrix.rotateM

I used the Grafika app and modified it to use the Camera2 API instead of the old API.我使用了 Grafika 应用程序并将其修改为使用 Camera2 API 而不是旧的 API。 I needed to use an OpenGL solution as I need to draw a watermark on top of the video, and Grafika was really useful.我需要使用 OpenGL 解决方案,因为我需要在视频顶部绘制水印,而 Grafika 非常有用。 Unfortunately, my output videos are recording with random "flickers" of frames in the wrong orientation.不幸的是,我的 output 视频正在以错误的方向随机“闪烁”帧进行录制。 I am looking to resolve the flickering issue, or at least understand why it is happening.我正在寻求解决闪烁的问题,或者至少了解它发生的原因。

Originally, I managed to successfully record video, with sound, and drawing the watermark on top, but the video was in the wrong orientation as I required portrait videos.最初,我设法成功录制了有声视频,并在顶部绘制了水印,但由于我需要纵向视频,因此视频的方向错误。 In order to achieve this, I used MediaMuxer.serOrientationHint() to configure the output file as portrait, and also applied a rotation to the transform matrix to ensure OpenGL frames were drawn in portrait, see below:为了实现这一点,我使用MediaMuxer.serOrientationHint()将 output 文件配置为纵向,并对变换矩阵应用旋转以确保 OpenGL 帧以纵向绘制,如下所示:

private void handleFrameAvailable(float[] transform, long timestampNanos) {

    mVideoEncoder.drainVideoEncoder(false);

    Matrix.rotateM(transform, 0, 270, 0, 0, 1); //Added these to rotate video frames
    Matrix.translateM(transform, 0, -1, 0, 0); //Added these to rotate video frames

    mFullScreen.drawFrame(mTextureId, transform);

    //...drawing of watermark happens here...//

    if (VERBOSE) { Log.e(TAG,"HandleVideo: "+timestampNanos); }
    mInputWindowSurface.setPresentationTime(timestampNanos);
    mInputWindowSurface.swapBuffers();
}

See below a regular frame and a glitched frame.请参阅下面的常规帧和故障帧。 In a 5 second video, around 20-30 non-consecutive frames may be like this.在一个 5 秒的视频中,大约 20-30 个不连续的帧可能是这样的。

好框架 坏帧

If you aren't resetting transform to identity matrix then you are accumulating transforms on each frame.如果您没有将变换重置为单位矩阵,那么您将在每一帧上累积变换。 Try:尝试:

Matrix.setIndentityM(transform, m);

before apply translation and rotation.在应用平移和旋转之前。

Besides, could be orientation:此外,可能是方向:

if (AppSetting.getValue(activity, Config.ORIENTATION, "").equalsIgnoreCase("Portrait")) {
    Matrix.rotateM(mTmpMatrix, 0, 270, 0, 0, 1);
    Matrix.translateM(mTmpMatrix, 0, -1, 0, 0);
}

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

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