Select H264 Profile when encoding with MediaCodec and MTK Codec

Paul Jackson picture Paul Jackson · Oct 8, 2014 · Viewed 7.4k times · Source

We have an Android app that encodes video into H264. On all previously tried Android devices this encodes to Baseline profile which is what I need.

On the Lenovo Yoga 10 the codec is OMX.MTK.VIDEO.ENCODER.AVC. This encodes the video as High Profile which gives a problem for the receiving device.

I am using MediaCodec. There seems to be no way to set the profile to be used.

Is there any way of doing this ? The codec does claim to support Baseline profile but gives no way of using it. Is there a codec specific parameter for this?

Answer

mstorsjo picture mstorsjo · Oct 10, 2014

What you could try is to add the key profile to your MediaFormat, with a value of 1 (OMX_VIDEO_AVCProfileBaseline). If you do this, you probably also need to add the key level with a level value matching your resolution as well (with a value from the OMX AVC level constants).

I'm not sure if this codec actually honors the requested value though, but it might be worth a try.

See the setupAVCEncoderParameters function in https://android.googlesource.com/platform/frameworks/av/+/6ade04174/media/libstagefright/ACodec.cpp for an example on the setup procedure. It looks for the profile key in the input parameters (which are copied from the MediaFormat that you've provided), but if this is present you also need to provide a level parameter, and what level to use depends on your resolution. See https://android.googlesource.com/platform/frameworks/native/+/cde4b13a/include/media/openmax/OMX_Video.h for constant values you can use for the parameters.

But after checking for the profile and level parameters, it also seems to override the profile to baseline regardless of what was set. So either these lines have been removed from your device, or the encoder disregards the h264type.eProfile field altogether.

If someone has got a source tree closer to what actually is used on the devices, it would be even better to inspect that.

As an example on how to pick a suitable level for your resolution, have a look at x264_validate_levels in http://git.videolan.org/?p=x264.git;a=blob;f=encoder/set.c;h=1a40b71284 (but the level passed to MediaFormat needs to be expressed using the OMX_VIDEO_AVCLEVELTYPE constants).

Not sure if anything of this helps, but it's at least worth a try.