Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set AVC/H.264 profile when encoding video in Android using MediaCodec API

Android provides a way to query the supported encoding profiles. But when setting up an encoder I cannot find the way to specify the desired profile to be used.

Finding supported profile/level pairs

Using the MediaCodec API in android you can call getCodecInfo() once you have chosen an encoder component. This returns a MediaCodecInfo object which provides details about the codec component being used. getCapabilitiesForType() returns a CodecCapabilities object, detailing what the codec is capable of. This contains an array of CodecProfileLevels which detail the supported profiles and levels which are supported.

Trying to set the profile

I can't see a field to set the profile for MedieCodec or for the MediaFormat.

There is a KEY_AAC_PROFILE for MediaFormat, but in the reference it explicitly states this is for aac audio only.

In MediaCodec there is a way to pass a Bundle of extra parameters to the codec using setParameters(). This looks like it has no generic documentation, and the parameters which are accepted will be different between codecs (different devices).

Background

Profiles specify a set of encoding features to use. Simple profiles are less computationally intense, but generally sacrifice quality for a given bit rate as a result. The levels specify the maximum resolution / bit-rate which are supported for a given profile. I expected levels usually to be associated with decoding capability, but since it is describing a hardware encoder which has to run in real time, having a maximum setting makes sense to me.

META: (I originally had a lot more links to each class + function I mentioned, but I had to remove them because I don't yet have the rep to post more than 2 links.)

like image 515
pauld Avatar asked Mar 24 '14 13:03

pauld


People also ask

Which H 264 profile should I use?

H. 264 High Profile is the most efficient and powerful profile in the H. 264 family, and is the primary profile for broadcast and disc storage, particularly for HDTV and Bluray disc storage formats.

Is AVC a codec?

264 is a video coding standard or codec. It was created by two standards bodies, the ITU-T Video Coding Experts Group, which called the standard H. 264, and the ISO/IEC JTC1 Moving Picture Experts Group (MPEG), which called the standard Advanced Video Coding (AVC).

What is Profile encoding video?

An encoding profile determines the audio/video quality of your encoded content. For example, it determines the resolution for each ray. Each ray is divided into slices. generated from your video.

How does MediaCodec work?

It processes data asynchronously and uses a set of input and output buffers. At a simplistic level, you request (or receive) an empty input buffer, fill it up with data and send it to the codec for processing. The codec uses up the data and transforms it into one of its empty output buffers.


2 Answers

For Android Kitkat devices we can set desired AVC profile and level into media format as per code snippet below(sets baseline with level 1.3). Please set this before starting MediaCodec.

format.setInteger(MediaFormat.KEY_PROFILE, MediaCodecInfo.CodecProfileLevel.AVCProfileBaseline);
format.setInteger(MediaFormat.KEY_LEVEL, MediaCodecInfo.CodecProfileLevel.AVCLevel13);
like image 145
Inderjeet Sharma Avatar answered Nov 10 '22 00:11

Inderjeet Sharma


There is no explicit profile selection in the MediaCodec API. Codec will select a profile on its own. It will also select a level based on input width/height and frame-rate numbers, but it may select a level that is higher than minimum required for the configuration.

You can query the encoder codec for the supported levels to see which profiles it may generate, but you will not be able to select a preferred one. Most likely the codec will select the highest profile it can handle for the given level, but there is no guarantee for that.

like image 31
Lajos Molnar Avatar answered Nov 09 '22 23:11

Lajos Molnar