谁能告诉我手机上用H265实时编码有什么鸟用?
一、先看看手机支持哪些codec
ALL_CODECS
REGULAR_CODECS
mine-type
选择mime-type为video/hevc,得到 <OMX.hisi.video.encoder.hevc>,这是华为海思芯片支持的h265硬编码,赞一个!
public static MediaCodecInfo selectCodec(String mimeType) { int numCodecs = MediaCodecList.getCodecCount(); for (int i = 0; i < numCodecs; i++) { MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i); if (!codecInfo.isEncoder()) { continue; } String[] types = codecInfo.getSupportedTypes(); for (int j = 0; j < types.length; j++) { if (types[j].equalsIgnoreCase(mimeType)) { return codecInfo; } } } return null; }
二、通过硬编码器进行编码
1. 先学习一下H265的帧,
NAL_UNIT_TYPE,其中SPS & PPS & SEI & IDR 的时间戳相同,参考H264的NAL单元详解
frame | 7 | 8 | 6 | 26 | 1 |
name | SPS | PPS | SEI | IDR | data |
description |
2. 配置编码器
String option = "bitrate=" + encodeBitrate; int pos = option.indexOf("bitrate="); if (pos != -1) { mEncodeBitrate = Integer.valueOf( (String) option.subSequence(pos + "bitrate=".length(), option.length())); Log.i(TAG, "setEncoderOption, mEncodeBitrate : " + mEncodeBitrate); } MediaFormat mediaformat = null; mediaformat = MediaFormat.createVideoFormat(H265_MIME, mWidth, mHeight); mediaformat.setInteger(MediaFormat.KEY_BIT_RATE, this.mEncodeBitrate); mediaformat.setInteger(MediaFormat.KEY_FRAME_RATE, encodeFrameRate); mediaformat.setInteger(MediaFormat.KEY_COLOR_FORMAT, encodeFormat); mediaformat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, iFrameInterval); mEncodeCodec.configure(mediaformat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);