Encoding H.264 from camera with Android MediaCodec

55,123

Solution 1

For your fast playback - frame rate issue, there is nothing you have to do here. Since it is a streaming solution the other side has to be told the frame rate in advance or timestamps with each frame. Both of these are not part of elementary stream. Either pre-determined framerate is chosen or you pass on some sdp or something like that or you use existing protocols like rtsp. In the second case the timestamps are part of the stream sent in form of something like rtp. Then the client has to depay the rtp stream and play it bacl. This is how elementary streaming works. [either fix your frame rate if you have a fixed rate encoder or give timestamps]

Local PC playback will be fast because it will not know the fps. By giving the fps parameter before the input e.g

ffplay -fps 30 in.264

you can control the playback on the PC.

As for the file not being playable: Does it have a SPS and PPS. Also you should have NAL headers enabled - annex b format. I don't know much about android, but this is requirement for any h.264 elementary stream to be playable when they are not in any containers and need to be dumped and played later. If android default is mp4, but default annexb headers will be switched off, so perhaps there is a switch to enable it. Or if you are getting data frame by frame, just add it yourself.

As for color format: I would guess the default should work. So try not setting it. If not try 422 Planar or UVYV / VYUY interleaved formats. usually cameras are one of those. (but not necessary, these may be the ones I have encountered more often).

Solution 2

Android 4.3 (API 18) provides an easy solution. The MediaCodec class now accepts input from Surfaces, which means you can connect the camera's Surface preview to the encoder and bypass all the weird YUV format issues.

There is also a new MediaMuxer class that will convert your raw H.264 stream to a .mp4 file (optionally blending in an audio stream).

See the CameraToMpegTest source for an example of doing exactly this. (It also demonstrates the use of an OpenGL ES fragment shader to perform a trivial edit on the video as it's being recorded.)

Solution 3

You can convert color spaces like this, if you have set the preview color space to YV12:

public static byte[] YV12toYUV420PackedSemiPlanar(final byte[] input, final byte[] output, final int width, final int height) {
        /* 
         * COLOR_TI_FormatYUV420PackedSemiPlanar is NV12
         * We convert by putting the corresponding U and V bytes together (interleaved).
         */
        final int frameSize = width * height;
        final int qFrameSize = frameSize/4;

        System.arraycopy(input, 0, output, 0, frameSize); // Y

        for (int i = 0; i < qFrameSize; i++) {
            output[frameSize + i*2] = input[frameSize + i + qFrameSize]; // Cb (U)
            output[frameSize + i*2 + 1] = input[frameSize + i]; // Cr (V)
        }
        return output;
    }

Or

 public static byte[] YV12toYUV420Planar(byte[] input, byte[] output, int width, int height) {
        /* 
         * COLOR_FormatYUV420Planar is I420 which is like YV12, but with U and V reversed.
         * So we just have to reverse U and V.
         */
        final int frameSize = width * height;
        final int qFrameSize = frameSize/4;

        System.arraycopy(input, 0, output, 0, frameSize); // Y
        System.arraycopy(input, frameSize, output, frameSize + qFrameSize, qFrameSize); // Cr (V)
        System.arraycopy(input, frameSize + qFrameSize, output, frameSize, qFrameSize); // Cb (U)

        return output;
    }

Solution 4

You can query the MediaCodec for it's supported bitmap format and query your preview. Problem is, some MediaCodecs only support proprietary packed YUV formats that you can't get from the preview. Particularly 2130706688 = 0x7F000100 = COLOR_TI_FormatYUV420PackedSemiPlanar . Default format for the preview is 17 = NV21 = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV411Planar = YCbCr 420 Semi Planar

Solution 5

If you did not explicitly request another pixel format, the camera preview buffers will arrive in a YUV 420 format known as NV21, for which COLOR_FormatYCrYCb is the MediaCodec equivalent.

Unfortunately, as other answers on this page mention, there is no guarantee that on your device, the AVC encoder supports this format. Note that there exist some strange devices that do not support NV21, but I don't know any that can be upgraded to API 16 (hence, have MediaCodec).

Google documentation also claims that YV12 planar YUV must be supported as camera preview format for all devices with API >= 12. Therefore, it may be useful to try it (the MediaCodec equivalent is COLOR_FormatYUV420Planar which you use in your code snippet).

Update: as Andrew Cottrell reminded me, YV12 still needs chroma swapping to become COLOR_FormatYUV420Planar.

Share:
55,123
gleerman
Author by

gleerman

Updated on August 21, 2020

Comments

  • gleerman
    gleerman over 3 years

    I'm trying to get this to work on Android 4.1 (using an upgraded Asus Transformer tablet). Thanks to Alex's response to my previous question, I already was able to write some raw H.264 data to a file, but this file is only playable with ffplay -f h264, and it seems like it's lost all information regarding the framerate (extremely fast playback). Also the color-space looks incorrect (atm using the camera's default on encoder's side).

    public class AvcEncoder {
    
    private MediaCodec mediaCodec;
    private BufferedOutputStream outputStream;
    
    public AvcEncoder() { 
        File f = new File(Environment.getExternalStorageDirectory(), "Download/video_encoded.264");
        touch (f);
        try {
            outputStream = new BufferedOutputStream(new FileOutputStream(f));
            Log.i("AvcEncoder", "outputStream initialized");
        } catch (Exception e){ 
            e.printStackTrace();
        }
    
        mediaCodec = MediaCodec.createEncoderByType("video/avc");
        MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 320, 240);
        mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 125000);
        mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 15);
        mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
        mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
        mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        mediaCodec.start();
    }
    
    public void close() {
        try {
            mediaCodec.stop();
            mediaCodec.release();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e){ 
            e.printStackTrace();
        }
    }
    
    // called from Camera.setPreviewCallbackWithBuffer(...) in other class
    public void offerEncoder(byte[] input) {
        try {
            ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
            ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
            int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
            if (inputBufferIndex >= 0) {
                ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
                inputBuffer.clear();
                inputBuffer.put(input);
                mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, 0, 0);
            }
    
            MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
            int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo,0);
            while (outputBufferIndex >= 0) {
                ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
                byte[] outData = new byte[bufferInfo.size];
                outputBuffer.get(outData);
                outputStream.write(outData, 0, outData.length);
                Log.i("AvcEncoder", outData.length + " bytes written");
    
                mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
                outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
    
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    
    }
    

    Changing the encoder type to "video/mp4" apparently solves the framerate-problem, but since the main goal is to make a streaming service, this is not a good solution.

    I'm aware that I dropped some of Alex' code considering the SPS and PPS NALU's, but I was hoping this would not be necessary since that information was also coming from outData and I assumed the encoder would format this correctly. If this is not the case, how should I arrange the different types of NALU's in my file/stream?

    So, what am I missing here in order to make a valid, working H.264 stream? And which settings should I use to make a match between the camera's colorspace and the encoder's colorspace?

    I have a feeling this is more of a H.264-related question than a Android/MediaCodec topic. Or am I still not using the MediaCodec API correctly?

    Thanks in advance.

    • David T.
      David T. over 11 years
      i've had a lot of issues with Android's media player, and also with how different Android phones behave differently. Is it possible for you to do the conversions server side?
    • n.dee
      n.dee over 11 years
      I'm working on similar functionality but am currently getting a java.nio.BufferOverflowException when calling offerEncoder(...) from Camera.setPreviewCallbackWithBuffer(...), would you be able to share the approach you took in Camera.setPreviewCallbackWithBuffer(...). Many thanks.
    • Nar Gar
      Nar Gar over 11 years
      The code seems to be ok except for java.nio.BufferOverflowException which happens on inputBuffer.put(input); statement. Tried to split the byte to buffer.capacity() chunks, ended up with an IllegalStateException error upon MediaCodec.queueInputBuffer call. Any idea how this can be rectified?
    • Nazar Merza
      Nazar Merza over 10 years
      Did you fixed the speed problem? If so, how?
  • Andrew Cottrell
    Andrew Cottrell over 10 years
    Very helpful. Your second function was exactly what I needed. Was able to grab the YV12 preview data and convert it to YUV420Planar for input to a MediaCodec encoder.
  • Andrew Cottrell
    Andrew Cottrell over 10 years
    In my experience, the YV12 preview data and YUV420Planar format are not equivalent. I had to use the YV12toYUV420Planar() function from another answer on this page to convert from one to the other.
  • Utkarsh Sinha
    Utkarsh Sinha over 10 years
    Something doesn't sound right in this answer. In the first code snipped, the method name is "YV12toYUV420PackedSemiPlanar". However, in the lines below you mention that "COLOR_TI_FormatYUV420PackedSemiPlanar is NV12".
  • dbro
    dbro over 10 years
    I've modified the CameraToMpegTest Compatibility Test Suite example into a regular Activity. github.com/OnlyInAmerica/HWEncoderExperiments
  • fadden
    fadden over 10 years
    And there's now a "show + capture camera" example in Grafika (github.com/google/grafika) that records the camera preview to .mp4 while simultaneously showing it on screen.
  • Rob Elsner
    Rob Elsner over 9 years
    Additionally some of them claim to support one format but deliver another (for instance NV12/NV21 are messed up on a lot of Samsung devices and some Samsung devices deliver one of the YV ones when you ask for an NV format). It's frustrating.
  • John
    John over 8 years
    @UtkarshSinha fourcc.org/yuv.php A good read, NV12 is YUV420. My problem was saving too few frames. Now I need to see the sun 2moro to check the colour.
  • ingsaurabh
    ingsaurabh over 7 years
    @fadden is this mp4 can be streamed simultaneously?