import 库
import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaCodecList;
MediaCodecList
MediaCodecList 将所有可用的codec 格式用MediaCodecInfo 对象的形式枚举出来,可通过其查找指定codec的相关信息,寻找支持特定format的encoder/decoder
MediaFormat
MeidaFormat :common keys
Name | Value Type | Description |
---|---|---|
KEY_MIME | String | The type of the format. |
KEY_CODECS_STRING | String | optional, the RFC 6381 codecs string of the MediaFormat |
KEY_MAX_INPUT_SIZE | Integer | optional, maximum size of a buffer of input data |
KEY_PIXEL_ASPECT_RATIO_WIDTH | Integer | optional, the pixel aspect ratio width |
KEY_PIXEL_ASPECT_RATIO_HEIGHT | Integer | optional, the pixel aspect ratio height |
KEY_BIT_RATE | Integer | encoder-only, desired bitrate in bits/second |
KEY_DURATION | long | the duration of the content (in microseconds) |
Video only keys:
Name | Value Type | Description |
---|---|---|
KEY_WIDTH | Integer | |
KEY_HEIGHT | Integer | |
KEY_COLOR_FORMAT | Integer | set by the user for encoders, readable in the output format of decoders |
KEY_FRAME_RATE | Integer or Float | required for encoders, optional for decoders |
KEY_CAPTURE_RATE | Integer | |
KEY_I_FRAME_INTERVAL | Integer (or Float) | encoder-only, time-interval between key frames. Float support added in Build.VERSION_CODES.N_MR1 |
KEY_INTRA_REFRESH_PERIOD | Integer | encoder-only, optional |
KEY_LATENCY | Integer | encoder-only, optional |
KEY_MAX_WIDTH | Integer | decoder-only, optional, max-resolution width |
KEY_MAX_HEIGHT | Integer | decoder-only, optional, max-resolution height |
KEY_REPEAT_PREVIOUS_FRAME_AFTER | Long | encoder in surface-mode only, optional |
KEY_PUSH_BLANK_BUFFERS_ON_STOP | Integer(1) | decoder rendering to a surface only, optional |
KEY_TEMPORAL_LAYERING | String | encoder only, optional, temporal-layering schema |
Audio only keys:
Name | Value Type | Description |
---|---|---|
KEY_CHANNEL_COUNT | Integer | |
KEY_SAMPLE_RATE | Integer | |
KEY_PCM_ENCODING | Integer | optional |
KEY_IS_ADTS | Integer | optional, if decoding AAC audio content, setting this key to 1 indicates that each audio frame is prefixed by the ADTS header. |
KEY_AAC_PROFILE | Integer | encoder-only, optional, if content is AAC audio, specifies the desired profile. |
KEY_AAC_SBR_MODE | Integer | encoder-only, optional, if content is AAC audio, specifies the desired SBR mode. |
KEY_AAC_DRC_TARGET_REFERENCE_LEVEL | Integer | decoder-only, optional, if content is AAC audio, specifies the target reference level. |
KEY_AAC_ENCODED_TARGET_LEVEL | Integer | decoder-only, optional, if content is AAC audio, specifies the target reference level used at encoder. |
KEY_AAC_DRC_BOOST_FACTOR | Integer | decoder-only, optional, if content is AAC audio, specifies the DRC boost factor. |
KEY_AAC_DRC_ATTENUATION_FACTOR | Integer | decoder-only, optional, if content is AAC audio, specifies the DRC attenuation factor. |
KEY_AAC_DRC_HEAVY_COMPRESSION | Integer | decoder-only, optional, if content is AAC audio, specifies whether to use heavy compression. |
KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT | Integer | decoder-only, optional, if content is AAC audio, specifies the maximum number of channels the decoder outputs. |
KEY_AAC_DRC_EFFECT_TYPE | Integer | decoder-only, optional, if content is AAC audio, specifies the MPEG-D DRC effect type to use. |
KEY_AAC_DRC_OUTPUT_LOUDNESS | Integer | decoder-only, optional, if content is AAC audio, returns the DRC output loudness. |
KEY_AAC_DRC_ALBUM_MODE | Integer | decoder-only, optional, if content is AAC audio, specifies the whether MPEG-D DRC Album Mode is active or not. |
KEY_CHANNEL_MASK | Integer | optional, a mask of audio channel assignments |
KEY_ENCODER_DELAY | Integer | optional, the number of frames to trim from the start of the decoded audio stream. |
KEY_ENCODER_PADDING | Integer | optional, the number of frames to trim from the end of the decoded audio stream. |
KEY_FLAC_COMPRESSION_LEVEL | Integer | encoder-only, optional, if content is FLAC audio, specifies the desired compression level. |
KEY_MPEGH_PROFILE_LEVEL_INDICATION | Integer | decoder-only, optional, if content is MPEG-H audio, specifies the profile and level of the stream. |
KEY_MPEGH_COMPATIBLE_SETS | ByteBuffer | decoder-only, optional, if content is MPEG-H audio, specifies the compatible sets (profile and level) of the stream. |
KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT | Integer | decoder-only, optional, if content is MPEG-H audio, specifies the preferred reference channel layout of the stream. |
Subtitle formats have the following keys:
Name | Value Type | Description |
---|---|---|
KEY_MIME | String | The type of the format. |
KEY_LANGUAGE | String | The language of the content. |
KEY_CAPTION_SERVICE_NUMBER | int | optional, the closed-caption service or channel number. |
Image formats have the following keys:
Name | Value Type | Description |
---|---|---|
KEY_MIME | String | The type of the format. |
KEY_WIDTH | Integer | |
KEY_HEIGHT | Integer | |
KEY_COLOR_FORMAT | Integer | set by the user for encoders, readable in the output format of decoders |
KEY_TILE_WIDTH | Integer | required if the image has grid |
KEY_TILE_HEIGHT | Integer | required if the image has grid |
KEY_GRID_ROWS | Integer | required if the image has grid |
KEY_GRID_COLUMNS | Integer | required if the image has grid |
配置编解码器
使用MediaCodec时首先要创建编解码器。 编解码器存在三种状态:停止、执行、释放;停止状态也包含三种子状态:未初始化的、已配置的、错误;执行状态也包含三种子状态:已刷新、正在运行、流结束。在初步创建时处于停止态的未初始化子态。
创建过程可使用下面几个函数完成
public static MediaCodec createByCodecName (String name);
public static MediaCodec createEncoderByType (String type);
public static MediaCodec createDecoderByType (String type);
顾名思义,第一个函数通过Codec Name 创建编解码器,后两个函数则通过制定type进行创建。
保险起见,通常创建编解码器时会在提供Codec Name 的基础上再提供一个type来提高容错。
而此处type则经常使用MediaFormat的KEY_MIME 属性。
查看对应函数源码如下
public static MediaCodec createDecoderByType(@NonNull String type)
throws IOException {
return new MediaCodec(type, true /* nameIsType */, false /* encoder */);
}
public static MediaCodec createEncoderByType(@NonNull String type)
throws IOException {
return new MediaCodec(type, true /* nameIsType */, true /* encoder */);
}
public static MediaCodec createByCodecName(@NonNull String name)
throws IOException {
return new MediaCodec(name, false /* nameIsType */, false /* encoder */);
}