Should I use ImageReader during decoding with MediaCodec to access Image

472 views Asked by At

I'm trying to decode frames, modify then, and encode to separate file. I have simplified code here:

            encoder = MediaCodec.createEncoderByType(MIME_TYPE);
            encoder.configure(outputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
            Surface surface2 = encoder.createInputSurface();
            encoder.start();
            final ImageWriter imageWriter = ImageWriter.newInstance(surface2, 30);
            ImageReader imageReader = ImageReader.newInstance(1920, 1080, ImageFormat.JPEG, 30);
            imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader reader) {
                    Image image = reader.acquireLatestImage();
                    // this image should be processed here, and passed to ImageWriter
                    imageWriter.queueInputImage(image);
                }
            }, null);

            // OutputSurface uses the EGL context created by InputSurface.
            decoder = MediaCodec.createDecoderByType(MIME_TYPE);
            decoder.configure(inputFormat, imageReader.getSurface(), null, 0);
            decoder.start();

But I've get following error:

2020-01-09 02:32:58.487 20349-20383/android.media.cts E/AndroidRuntime: FATAL EXCEPTION: codec test
    Process: android.media.cts, PID: 20349
    java.lang.UnsupportedOperationException: The producer output buffer format 0x7fa30c06 doesn't match the ImageReader's configured buffer format 0x21.
        at android.media.ImageReader.nativeImageSetup(Native Method)
        at android.media.ImageReader.acquireNextSurfaceImage(ImageReader.java:450)
        at android.media.ImageReader.acquireNextImage(ImageReader.java:504)
        at android.media.ImageReader.acquireLatestImage(ImageReader.java:397)

Is my code is correct construction of decoding-modifying-encoding videos in android?

0

There are 0 answers