誰能告訴我手機上用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);