How to use MediaCodec without MediaExtractor for H264

前端 未结 2 1677
一向
一向 2020-11-29 21:22

I need to use MediaCodec without the MediaExtractor and I\'m reading the file using a FileInputStream. Currently it is not working, it is showing a greenish scrambled image

相关标签:
2条回答
  • 2020-11-29 21:41

    I'm assuming you're reading a raw H.264 elementary stream and not an MP4 file.

    It looks like you're feeding fixed-size blocks of data to the decoder. That doesn't work. You need to put a single access unit into each buffer.

    0 讨论(0)
  • 2020-11-29 21:47

    To your last question i.e. how can you get SPS and PPS values, you need to have a mechanism to read the same from the file.

    If you are having an elementary stream file, then you would need to parse the file, identify NALU headers and extract the content.

    If you have container format, you will need to have a mechanism to read the file format of the container type and extract the information.

    If you have a streaming input, then you can receive the content from the incoming SDP information.

    As for your current code, I would recommend concatenating both SPS and PPS into one buffer and provide the same to the underlying codec as shown below

    byte[] csd_info = { 0, 0, 0, 1, 103, 100, 0, 40, -84, 52, -59, 1, -32, 17, 31, 120, 11, 80, 16, 16, 31, 0, 0, 3, 3, -23, 0, 0, -22, 96, -108, 0, 0, 0, 1, 104, -18, 60, -128 };
    format.setByteBuffer("csd-0", ByteBuffer.wrap(csd_info));
    format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 1920 * 1080);
    format.setInteger("durationUs", 63446722);
    
    0 讨论(0)
提交回复
热议问题