The NAL length prefix that indicates the length of the NAL block must be converted to the application-B initial codes (bytes 0x00, 0x00, 0x00, 0x01) before proceeding to MediaCodec for decoding. (Some decoders can immediately accept the MP4 format, but this is not too common.)
SPS / PPS, which is stored in the avcC atom in a file, also needs to be converted in order to use the application-B initial codes. Please note that avcC atom contains several other fields that you do not need to pass to the decoder. You can transfer SPS and PPS in one buffer (with initial codes in front of each of them) with the BUFFER_FLAG_CODEC_CONFIG flag before sending any real frames or transfer them (using the initial codes of application-B) to MediaFormat, which you use to configure the decoder (or in one ByteBuffer with the key "csd-0", or two separate keys "csd-0" and "csd-1").
If your file has more SPS / PPS inside each frame, you should simply transfer them as part of the frame, and most decoders should be able to handle it (especially if it is the same SPS / PPS as before and after the configuration change).
Thus: transfer all NAL units belonging to the same sample in the same buffer, but all the NAL header headers are overwritten with initial codes. And to work with MP4 files that do not have SPS / PPS inside the stream itself, analyze avcC atom (I donβt know in what format mp4parser returns this) and transfer SPS and PPS with initial codes to the decoder (or via MediaFormat as "csd- 0 ", or as the first buffer with the setting BUFFER_FLAG_CODEC_CONFIG).
source share