Configure MediaCodec with the proper MediaFormat from a raw H.264 byte buffer

889 views Asked by At

I have to decode an H.264 stream with the Android MediaCodec API. Problem is I don't know at this point the size of the output stream (I mean resolution of frames : 640x360? 1280x720? 1920x1080? ...). I use my own access/demuxer code to get the input stream as a ByteBuffer object. First step to create my MediaCodec is:

MediaCodec mediaCodec = MediaCodec.createDecoderByType("video/avc");

Then, to configure it, I would like to have the proper MediaFormat information to configure my MediaCodec object fine. Currently, I do:

MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 1280, 720);

But the size is not always 1280x720 and my MediaFormat needs more information (on some test devices, decoder produces weird results but works fine on others). I read the doc and I saw that we can get the proper MediaFormat using a MediaExtractor and getTrackFormat(/* idx */);. But how can I use MediaExtractor with my raw ByteBuffer (setDataSource(...) can only handle file descriptors or uris)? Is there an other way to process?

Thanks!

EDIT: I extracted SPS and PPS from my stream and provided it to the codec at the configuration step (passing by the MediaFormat) doing:

mediaFormat.setByteBuffer("csd-0", bufferSPS);
mediaFormat.setByteBuffer("csd-1", bufferPPS);
/*...*/
mediaCodec.configure(mediaFormat, null, null, 0);
0

There are 0 answers