Encoding bitmaps with MediaCodec using surface and OpenGL causing application crash unexpectedly

0

Am using MediaCodec and MediaExtractor to decode a video file. After decoding, each decoded frame is converting to bitmap to edit and feeding the edited bitmap to mediacodec encoder to create a video file using Surface and OpenGL. At the end of processing, the application is closing with the following errors

2021-02-07 11:12:00.664 673-993/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) dequeueBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.669 1059-5294/? E/QC2Buf: allocate: failed to alloc graphic block w(1920)/h(1088)/f(7fa30c06)/usage(20010000)
2021-02-07 11:12:00.670 1059-5294/? E/QC2V4l2Codec: [avcD_303] Error during buffer allocation
2021-02-07 11:12:00.672 673-706/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) dequeueBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.673 1059-5294/? E/QC2Buf: allocate: failed to alloc graphic block w(1920)/h(1088)/f(7fa30c06)/usage(20010000)
2021-02-07 11:12:00.673 1059-5294/? E/QC2V4l2Codec: [avcD_303] Error during buffer allocation
2021-02-07 11:12:00.673 673-706/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) dequeueBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.674 1059-5294/? E/QC2Buf: allocate: failed to alloc graphic block w(1920)/h(1088)/f(7fa30c06)/usage(20010000)
2021-02-07 11:12:00.674 1059-5294/? E/QC2V4l2Codec: [avcD_303] Error during buffer allocation
2021-02-07 11:12:00.674 673-706/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) dequeueBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.674 1059-5294/? E/QC2Buf: allocate: failed to alloc graphic block w(1920)/h(1088)/f(7fa30c06)/usage(20010000)
2021-02-07 11:12:00.674 1059-5294/? E/QC2V4l2Codec: [avcD_303] Error during buffer allocation
2021-02-07 11:12:00.723 673-706/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.723 673-993/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.725 673-993/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.725 673-706/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.726 673-3949/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.728 673-3949/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.728 673-706/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.728 673-3949/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.730 673-706/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.731 673-706/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.731 673-3949/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.732 673-706/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.732 673-3949/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.732 673-993/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.733 673-705/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.733 673-3949/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.733 673-3151/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.736 1225-5290/? E/MediaCodec: Codec reported err 0x80000000, actionCode 0, while in state 7
2021-02-07 11:12:00.754 1225-5289/? E/NuPlayerDecoder: failed to flush [c2.qti.avc.decoder] (err=-2147483648)
2021-02-07 11:12:00.755 1225-5285/? E/NuPlayer: received error(0x80000000) from video decoder, flushing(2), now shutting down
2021-02-07 11:12:00.765 5492-5492/? E/earchbox:searc: Not starting debugger since process cannot load the jdwp agent.
2021-02-07 11:12:00.774 673-3949/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) connect: BufferQueue has been abandoned
2021-02-07 11:12:00.777 1225-5289/? E/SurfaceUtils: Failed to connect to surface 0xe8701948, err -19
2021-02-07 11:12:00.777 673-3949/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.779 673-3151/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.780 673-3949/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.782 673-993/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) cancelBuffer: BufferQueue has been abandoned
2021-02-07 11:12:00.791 673-993/? E/BufferQueueProducer: [SurfaceView - com.visym.collector/com.visym.collector.capturemodule.views.VideoPreviewActivity#0](id:2a1000002e2,api:3,p:1225,c:673) detachBuffer: BufferQueue has been abandoned

Following are the lines of code for rendering bitmaps using opengl

    private void generateSurfaceFrame(Bitmap bitmap) {
        GLES20.glClearColor(0, 0, 0, 1);
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

        int texture = createTexture(bitmap);
        Log.d(TAG, "generateSurfaceFrame: rendering started");
        Util.renderTexture(texture, bitmap.getWidth(),bitmap.getHeight());
        Log.d(TAG, "generateSurfaceFrame: rendering finished");
    }

    public static int createTexture(Bitmap bitmap) {
        int[] textures = new int[1];
        GLES20.glGenTextures(textures.length, textures, 0);
        int texture = textures[0];
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
        GLES20.glTexParameteri(
                GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(
                GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(
                GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(
                GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
        return texture;
    }



public class Util {

    private static final String VERTEX_SHADER =
            "attribute vec4 a_position;\n" +
                    "attribute vec2 a_texcoord;\n" +
                    "varying vec2 v_texcoord;\n" +
                    "void main() {\n" +
                    "  gl_Position = a_position;\n" +
                    "  v_texcoord = a_texcoord;\n" +
                    "}\n";
    private static final String FRAGMENT_SHADER =
            "precision mediump float;\n" +
                    "uniform sampler2D tex_sampler;\n" +
                    "varying vec2 v_texcoord;\n" +
                    "void main() {\n" +
                    "  gl_FragColor = texture2D(tex_sampler, v_texcoord);\n" +
                    "}\n";
    private static final float[] TEX_VERTICES = {
            0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f
    };
    private static final float[] POS_VERTICES = {
            -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f
    };

    public static void renderTexture(int texture, int viewWidth, int viewHeight) {
        RenderContext context = createProgram();
        if (context == null) {
            return;
        }
        // Use our shader program
        GLES20.glUseProgram(context.shaderProgram);
        // Set viewport
        GLES20.glViewport(0, 0, viewWidth, viewHeight);
        // Disable blending
        GLES20.glDisable(GLES20.GL_BLEND);
        // Set the vertex attributes
        GLES20.glVertexAttribPointer(
                context.texCoordHandle, 2, GLES20.GL_FLOAT, false, 0, context.texVertices);
        GLES20.glEnableVertexAttribArray(context.texCoordHandle);
        GLES20.glVertexAttribPointer(
                context.posCoordHandle, 2, GLES20.GL_FLOAT, false, 0, context.posVertices);
        GLES20.glEnableVertexAttribArray(context.posCoordHandle);
        // Set the input texture
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
        GLES20.glUniform1i(context.texSamplerHandle, 0);
        // Draw!
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    }

    public static RenderContext createProgram() {
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER);
        if (vertexShader == 0) {
            return null;
        }
        int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER);
        if (pixelShader == 0) {
            return null;
        }
        int program = GLES20.glCreateProgram();
        if (program != 0) {
            GLES20.glAttachShader(program, vertexShader);
            GLES20.glAttachShader(program, pixelShader);
            GLES20.glLinkProgram(program);
            int[] linkStatus = new int[1];
            GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
            if (linkStatus[0] != GLES20.GL_TRUE) {
                String info = GLES20.glGetProgramInfoLog(program);
                GLES20.glDeleteProgram(program);
                program = 0;
                throw new RuntimeException("Could not link program: " + info);
            }
        }

        // Bind attributes and uniforms
        RenderContext context = new RenderContext();
        context.texSamplerHandle = GLES20.glGetUniformLocation(program, "tex_sampler");
        context.texCoordHandle = GLES20.glGetAttribLocation(program, "a_texcoord");
        context.posCoordHandle = GLES20.glGetAttribLocation(program, "a_position");
        context.texVertices = createVerticesBuffer(TEX_VERTICES);
        context.posVertices = createVerticesBuffer(POS_VERTICES);
        context.shaderProgram = program;
        return context;
    }

    private static int loadShader(int shaderType, String source) {
        int shader = GLES20.glCreateShader(shaderType);
        if (shader != 0) {
            GLES20.glShaderSource(shader, source);
            GLES20.glCompileShader(shader);
            int[] compiled = new int[1];
            GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
            if (compiled[0] == 0) {
                String info = GLES20.glGetShaderInfoLog(shader);
                GLES20.glDeleteShader(shader);
                shader = 0;
                throw new RuntimeException("Could not compile shader " + shaderType + ":" + info);
            }
        }
        return shader;
    }

    private static FloatBuffer createVerticesBuffer(float[] vertices) {
        if (vertices.length != 8) {
            throw new RuntimeException("Number of vertices should be four.");
        }
        FloatBuffer buffer = ByteBuffer.allocateDirect(
                vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
        buffer.put(vertices).position(0);
        return buffer;
    }

    public static class RenderContext {
        private int shaderProgram;
        private int texSamplerHandle;
        private int texCoordHandle;
        private int posCoordHandle;
        private FloatBuffer texVertices;
        private FloatBuffer posVertices;
    }
}

But on some devices, it works fine without any error. Kindly let me know what might be the reason to cause these error messages. I am stuck to find the actual issue.

android
opengl-es
android-mediacodec
asked on Stack Overflow Feb 4, 2021 by Akshay Mohana • edited Feb 7, 2021 by Akshay Mohana

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0