MediaCodec个人学习

本文介绍了Android中MediaCodec库的使用,包括MediaCodecList用于获取所有可用编解码器的信息,MediaFormat用于定义编解码格式。内容详细阐述了MediaFormat的常见键值,以及如何配置和创建MediaCodec,特别强调了创建过程中使用Codec Name和type的重要性。
摘要由CSDN通过智能技术生成

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

NameValue TypeDescription
KEY_MIMEStringThe type of the format.
KEY_CODECS_STRINGStringoptional, the RFC 6381 codecs string of the MediaFormat
KEY_MAX_INPUT_SIZEIntegeroptional, maximum size of a buffer of input data
KEY_PIXEL_ASPECT_RATIO_WIDTHIntegeroptional, the pixel aspect ratio width
KEY_PIXEL_ASPECT_RATIO_HEIGHTIntegeroptional, the pixel aspect ratio height
KEY_BIT_RATEIntegerencoder-only, desired bitrate in bits/second
KEY_DURATIONlongthe duration of the content (in microseconds)

Video only keys:

NameValue TypeDescription
KEY_WIDTHInteger
KEY_HEIGHTInteger
KEY_COLOR_FORMATIntegerset by the user for encoders, readable in the output format of decoders
KEY_FRAME_RATEInteger or Floatrequired for encoders, optional for decoders
KEY_CAPTURE_RATEInteger
KEY_I_FRAME_INTERVALInteger (or Float)encoder-only, time-interval between key frames. Float support added in Build.VERSION_CODES.N_MR1
KEY_INTRA_REFRESH_PERIODIntegerencoder-only, optional
KEY_LATENCYIntegerencoder-only, optional
KEY_MAX_WIDTHIntegerdecoder-only, optional, max-resolution width
KEY_MAX_HEIGHTIntegerdecoder-only, optional, max-resolution height
KEY_REPEAT_PREVIOUS_FRAME_AFTERLongencoder in surface-mode only, optional
KEY_PUSH_BLANK_BUFFERS_ON_STOPInteger(1)decoder rendering to a surface only, optional
KEY_TEMPORAL_LAYERINGStringencoder only, optional, temporal-layering schema

Audio only keys:

NameValue TypeDescription
KEY_CHANNEL_COUNTInteger
KEY_SAMPLE_RATEInteger
KEY_PCM_ENCODINGIntegeroptional
KEY_IS_ADTSIntegeroptional, if decoding AAC audio content, setting this key to 1 indicates that each audio frame is prefixed by the ADTS header.
KEY_AAC_PROFILEIntegerencoder-only, optional, if content is AAC audio, specifies the desired profile.
KEY_AAC_SBR_MODEIntegerencoder-only, optional, if content is AAC audio, specifies the desired SBR mode.
KEY_AAC_DRC_TARGET_REFERENCE_LEVELIntegerdecoder-only, optional, if content is AAC audio, specifies the target reference level.
KEY_AAC_ENCODED_TARGET_LEVELIntegerdecoder-only, optional, if content is AAC audio, specifies the target reference level used at encoder.
KEY_AAC_DRC_BOOST_FACTORIntegerdecoder-only, optional, if content is AAC audio, specifies the DRC boost factor.
KEY_AAC_DRC_ATTENUATION_FACTORIntegerdecoder-only, optional, if content is AAC audio, specifies the DRC attenuation factor.
KEY_AAC_DRC_HEAVY_COMPRESSIONIntegerdecoder-only, optional, if content is AAC audio, specifies whether to use heavy compression.
KEY_AAC_MAX_OUTPUT_CHANNEL_COUNTIntegerdecoder-only, optional, if content is AAC audio, specifies the maximum number of channels the decoder outputs.
KEY_AAC_DRC_EFFECT_TYPEIntegerdecoder-only, optional, if content is AAC audio, specifies the MPEG-D DRC effect type to use.
KEY_AAC_DRC_OUTPUT_LOUDNESSIntegerdecoder-only, optional, if content is AAC audio, returns the DRC output loudness.
KEY_AAC_DRC_ALBUM_MODEIntegerdecoder-only, optional, if content is AAC audio, specifies the whether MPEG-D DRC Album Mode is active or not.
KEY_CHANNEL_MASKIntegeroptional, a mask of audio channel assignments
KEY_ENCODER_DELAYIntegeroptional, the number of frames to trim from the start of the decoded audio stream.
KEY_ENCODER_PADDINGIntegeroptional, the number of frames to trim from the end of the decoded audio stream.
KEY_FLAC_COMPRESSION_LEVELIntegerencoder-only, optional, if content is FLAC audio, specifies the desired compression level.
KEY_MPEGH_PROFILE_LEVEL_INDICATIONIntegerdecoder-only, optional, if content is MPEG-H audio, specifies the profile and level of the stream.
KEY_MPEGH_COMPATIBLE_SETSByteBufferdecoder-only, optional, if content is MPEG-H audio, specifies the compatible sets (profile and level) of the stream.
KEY_MPEGH_REFERENCE_CHANNEL_LAYOUTIntegerdecoder-only, optional, if content is MPEG-H audio, specifies the preferred reference channel layout of the stream.

Subtitle formats have the following keys:

NameValue TypeDescription
KEY_MIMEStringThe type of the format.
KEY_LANGUAGEStringThe language of the content.
KEY_CAPTION_SERVICE_NUMBERintoptional, the closed-caption service or channel number.

Image formats have the following keys:

NameValue TypeDescription
KEY_MIMEStringThe type of the format.
KEY_WIDTHInteger
KEY_HEIGHTInteger
KEY_COLOR_FORMATIntegerset by the user for encoders, readable in the output format of decoders
KEY_TILE_WIDTHIntegerrequired if the image has grid
KEY_TILE_HEIGHTIntegerrequired if the image has grid
KEY_GRID_ROWSIntegerrequired if the image has grid
KEY_GRID_COLUMNSIntegerrequired 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 */);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值