Search code examples
androidandroid-mediacodechevc

MediaMuxer HEVC "no codec config"


Trying to mux HEVC encoded video to a MediaMuxer MPEG-4 container on a Samsung Galaxy S6.

MediaFormat (as obtained from MediaCodec):

{height=1080, width=1920, max_capacity=8847360, what=1869968451, mime=video/hevc, csd-0=java.nio.ByteArrayBuffer[position=0,limit=76,capacity=76], remained_resource=6773760}

csd-0 appears in-there, and its all that's enough for H265/hevc, according to the table in the docs

MediaMuxer creates video track with index 0 based on this format. After writing some samples and then calling mediaMuxer.stop(), IllegalStateException is thrown and following shows up in logcat:

E/MPEG4Writer: Missing codec specific data
D/MPEG4Writer: Video track stopping
D/MPEG4Writer: Video track source stopping
D/MPEG4Writer: Video track source stopped
I/MPEG4Writer: Received total/0-length (247/0) buffers and encoded 247 frames. - Audio
I/MPEG4Writer: Audio track drift time: 0 us
I/MPEG4Writer: Received total/0-length (173/0) buffers and encoded 173 frames. - Video
D/MPEG4Writer: Video track stopped
D/MPEG4Writer: Audio track stopping
D/MPEG4Writer: Audio track source stopping
D/MPEG4Writer: Audio track source stopped
D/MPEG4Writer: Audio track stopped
D/MPEG4Writer: Duration from tracks range is [5744606, 5764430] us
D/MPEG4Writer: Stopping writer thread
D/MPEG4Writer: 0 chunks are written in the last batch
D/MPEG4Writer: Writer thread stopped
E/MP4FileMuxer: finish: stop or release failed
java.lang.IllegalStateException: Failed to stop the muxer

Generated MP4 file is unplayable. What can be the problem?


Solution

  • So, to answer my own question: it seems that "video/hevc" is only supported by MediaMuxer starting with Android 7.0 r1 (will NOT work even with N-preview-5). Looks like the documentation for MediaMuxer.addTrack() has been updated to reflect this.

    Relevant AOSP code:

    MPEG4Writer.cpp on N preview 5 - no HEVC support

    MPEG4Writer.cpp on N r1 - HEVC bits and pieces everywhere

    Unfortunately, there are devices with Marshmallow that support HEVC video encoding (e.g. Samsung Exynos), but using a MediaMuxer to dump that into an MP4 file will not work, so a custom solution is required.