Skip to content

Commit f0f06f8

Browse files
committed
新增日志,以及一些降级处理
1 parent 23ec8c2 commit f0f06f8

7 files changed

Lines changed: 264 additions & 83 deletions

File tree

app/src/main/java/com/demo/mediacodec/MediaCodecUtils.java

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public static String findDecoderByFormat(MediaFormat mediaFormat, boolean onlySo
5050
}
5151
String mimeType = mediaFormat.getString(MediaFormat.KEY_MIME);
5252
try {
53-
MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(mimeType);
53+
MediaCodecInfo.CodecCapabilities capabilities =
54+
codecInfo.getCapabilitiesForType(mimeType);
5455
if (capabilities.isFormatSupported(mediaFormat)) {
5556
return codecInfo.getName();
5657
}
@@ -82,7 +83,8 @@ public static String findEncoderByFormat(MediaFormat mediaFormat, boolean onlySo
8283
}
8384
String mimeType = mediaFormat.getString(MediaFormat.KEY_MIME);
8485
try {
85-
MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(mimeType);
86+
MediaCodecInfo.CodecCapabilities capabilities =
87+
codecInfo.getCapabilitiesForType(mimeType);
8688
if (capabilities.isFormatSupported(mediaFormat)) {
8789
return codecInfo.getName();
8890
}
@@ -142,14 +144,17 @@ public enum EGLColorSpace {
142144
}
143145

144146
@NonNull
145-
public static MediaFormat createOutputFormat(@NonNull Context ctx, @NonNull Uri srcUri, @NonNull MediaFormat mOriVideoFormat, @NonNull TranscodeConfig config, @NonNull VideoOutputConfig outputConfig) {
147+
public static MediaFormat createOutputFormat(@NonNull Context ctx, @NonNull Uri srcUri,
148+
@NonNull MediaFormat mOriVideoFormat,
149+
@NonNull TranscodeConfig config,
150+
@NonNull VideoOutputConfig outputConfig) {
146151
MediaFormat outputFormat;
147152
String inMimeType = mOriVideoFormat.getString(MediaFormat.KEY_MIME);
148153
boolean isH265 = false;
149154
String mime;
150155
if (config.h265) {
151156
isH265 = true;
152-
if (MediaFormat.MIMETYPE_VIDEO_DOLBY_VISION.equals(inMimeType) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
157+
if (outputConfig.outputLevel != OutputLevel.NO_HDR && MediaFormat.MIMETYPE_VIDEO_DOLBY_VISION.equals(inMimeType) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
153158
//如果是杜比视界,那么需要检查能否使用杜比视界的mimeType
154159
mime = MediaFormat.MIMETYPE_VIDEO_DOLBY_VISION;
155160
outputConfig.isDolby = true;
@@ -165,7 +170,8 @@ public static MediaFormat createOutputFormat(@NonNull Context ctx, @NonNull Uri
165170
if (codecName == null) {
166171
//说明没有杜比视界的编码器,降级到Hevc去
167172
mime = MediaFormat.MIMETYPE_VIDEO_HEVC;
168-
outputFormat = MediaFormat.createVideoFormat(mime, config.outWidth, config.outHeight);
173+
outputFormat = MediaFormat.createVideoFormat(mime, config.outWidth,
174+
config.outHeight);
169175
outputConfig.isDolby = false;
170176
}
171177
}
@@ -190,51 +196,56 @@ public static MediaFormat createOutputFormat(@NonNull Context ctx, @NonNull Uri
190196
//设置Color相关参数,使其尽量保证HDR视频转码后仍然是HDR视频
191197
int colorTransfer = 0;
192198
int colorStandard = mOriVideoFormat.getInteger(MediaFormat.KEY_COLOR_STANDARD);
193-
outputConfig.isHDR = colorStandard == MediaFormat.COLOR_STANDARD_BT2020;
199+
outputConfig.isHDR =
200+
outputConfig.outputLevel != OutputLevel.NO_HDR && colorStandard == MediaFormat.COLOR_STANDARD_BT2020;
194201
if (outputConfig.isHDR) {
195202
outputConfig.isHDRVivid = MediaCodecUtils.isHDRVivid(ctx, null, srcUri, null);
196203
}
197-
if (mOriVideoFormat.containsKey(MediaFormat.KEY_COLOR_STANDARD)) {
198-
outputFormat.setInteger(MediaFormat.KEY_COLOR_STANDARD, colorStandard);
199-
}
200-
if (mOriVideoFormat.containsKey(MediaFormat.KEY_COLOR_TRANSFER)) {
201-
colorTransfer = mOriVideoFormat.getInteger(MediaFormat.KEY_COLOR_TRANSFER);
202-
outputFormat.setInteger(MediaFormat.KEY_COLOR_TRANSFER, colorTransfer);
203-
}
204-
if (mOriVideoFormat.containsKey(MediaFormat.KEY_COLOR_RANGE)) {
205-
outputFormat.setInteger(MediaFormat.KEY_COLOR_RANGE,
206-
mOriVideoFormat.getInteger(MediaFormat.KEY_COLOR_RANGE));
207-
}
208-
if (mOriVideoFormat.containsKey(MediaFormat.KEY_HDR_STATIC_INFO)) {
209-
outputFormat.setByteBuffer(MediaFormat.KEY_HDR_STATIC_INFO,
210-
mOriVideoFormat.getByteBuffer(MediaFormat.KEY_HDR_STATIC_INFO));
211-
}
212-
if (outputConfig.isDolby) {
213-
//如果是杜比
214-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
215-
outputFormat.setInteger(MediaFormat.KEY_PROFILE,
216-
MediaCodecInfo.CodecProfileLevel.DolbyVisionProfileDvheSt);
217-
} else {
218-
outputFormat.setInteger(MediaFormat.KEY_PROFILE,
219-
MediaCodecInfo.CodecProfileLevel.DolbyVisionProfileDvheStn);
204+
if (outputConfig.isHDR) {
205+
if (mOriVideoFormat.containsKey(MediaFormat.KEY_COLOR_STANDARD)) {
206+
outputFormat.setInteger(MediaFormat.KEY_COLOR_STANDARD, colorStandard);
220207
}
221-
} else {
222-
outputFormat.setFeatureEnabled("hdr-editing", true);
223-
switch (colorTransfer) {
224-
case MediaFormat.COLOR_TRANSFER_HLG:
225-
//HLG(HGL10)
226-
outputFormat.setInteger(MediaFormat.KEY_PROFILE,
227-
MediaCodecInfo.CodecProfileLevel.HEVCProfileMain10);
228-
break;
229-
case MediaFormat.COLOR_TRANSFER_ST2084:
230-
//PQ(HDR10和HDR10+)
231-
//TODO 怎么区分HDR10和HDR10+ HEVCProfileMain10HDR10Plus
232-
outputFormat.setInteger(MediaFormat.KEY_PROFILE,
233-
MediaCodecInfo.CodecProfileLevel.HEVCProfileMain10HDR10);
234-
//TODO 可能还是要降级成HEVCProfileMain10
235-
break;
236-
default:
237-
break;
208+
if (mOriVideoFormat.containsKey(MediaFormat.KEY_COLOR_TRANSFER)) {
209+
colorTransfer = mOriVideoFormat.getInteger(MediaFormat.KEY_COLOR_TRANSFER);
210+
outputFormat.setInteger(MediaFormat.KEY_COLOR_TRANSFER, colorTransfer);
211+
}
212+
if (mOriVideoFormat.containsKey(MediaFormat.KEY_COLOR_RANGE)) {
213+
outputFormat.setInteger(MediaFormat.KEY_COLOR_RANGE,
214+
mOriVideoFormat.getInteger(MediaFormat.KEY_COLOR_RANGE));
215+
}
216+
if (mOriVideoFormat.containsKey(MediaFormat.KEY_HDR_STATIC_INFO)) {
217+
outputFormat.setByteBuffer(MediaFormat.KEY_HDR_STATIC_INFO,
218+
mOriVideoFormat.getByteBuffer(MediaFormat.KEY_HDR_STATIC_INFO));
219+
}
220+
if (outputConfig.outputLevel != OutputLevel.NO_PROFILE) {
221+
if (outputConfig.isDolby) {
222+
//如果是杜比
223+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
224+
outputFormat.setInteger(MediaFormat.KEY_PROFILE,
225+
MediaCodecInfo.CodecProfileLevel.DolbyVisionProfileDvheSt);
226+
} else {
227+
outputFormat.setInteger(MediaFormat.KEY_PROFILE,
228+
MediaCodecInfo.CodecProfileLevel.DolbyVisionProfileDvheStn);
229+
}
230+
} else {
231+
outputFormat.setFeatureEnabled("hdr-editing", true);
232+
switch (colorTransfer) {
233+
case MediaFormat.COLOR_TRANSFER_HLG:
234+
//HLG(HGL10)
235+
outputFormat.setInteger(MediaFormat.KEY_PROFILE,
236+
MediaCodecInfo.CodecProfileLevel.HEVCProfileMain10);
237+
break;
238+
case MediaFormat.COLOR_TRANSFER_ST2084:
239+
//PQ(HDR10和HDR10+)
240+
//TODO 怎么区分HDR10和HDR10+ HEVCProfileMain10HDR10Plus
241+
outputFormat.setInteger(MediaFormat.KEY_PROFILE,
242+
MediaCodecInfo.CodecProfileLevel.HEVCProfileMain10HDR10);
243+
//TODO 可能还是要降级成HEVCProfileMain10
244+
break;
245+
default:
246+
break;
247+
}
248+
}
238249
}
239250
}
240251
}

app/src/main/java/com/demo/mediacodec/transcode/InputSurface.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,12 @@ private void eglSetup(VideoOutputConfig config) {
6565
createSdrEGLContextAndWindow();
6666
config.eglColorSpace = MediaCodecUtils.EGLColorSpace.RGB888;
6767
} else {
68-
try {
69-
if (config.isDolby) {
70-
//TODO
71-
config.eglColorSpace = MediaCodecUtils.EGLColorSpace.RGBA1010102;
72-
} else if (config.isHDRVivid) {
73-
//TODO
74-
createYUVP10EGLContextAndWindow();
75-
config.eglColorSpace = MediaCodecUtils.EGLColorSpace.YUVP10;
76-
}
77-
} catch (Exception e) {
78-
e.printStackTrace();
79-
createSdrEGLContextAndWindow();
80-
config.eglColorSpace = MediaCodecUtils.EGLColorSpace.RGB888;
68+
if (config.isDolby) {
69+
createRGBA1010102EGLContextAndWindow();
70+
config.eglColorSpace = MediaCodecUtils.EGLColorSpace.RGBA1010102;
71+
} else if (config.isHDRVivid) {
72+
createYUVP10EGLContextAndWindow();
73+
config.eglColorSpace = MediaCodecUtils.EGLColorSpace.YUVP10;
8174
}
8275
}
8376

@@ -237,6 +230,38 @@ private void createSdrEGLContextAndWindow() {
237230
createEGLSurface();
238231
}
239232

233+
private void createRGBA1010102EGLContextAndWindow() {
234+
//TODO 需要检查是否支持
235+
int[] attribList = {
236+
EGL14.EGL_RED_SIZE, 10,
237+
EGL14.EGL_GREEN_SIZE, 10,
238+
EGL14.EGL_BLUE_SIZE, 10,
239+
EGL14.EGL_ALPHA_SIZE, 2,
240+
EGL14.EGL_RENDERABLE_TYPE, EGLExt.EGL_OPENGL_ES3_BIT_KHR,
241+
EGL14.EGL_NONE
242+
};
243+
int[] numConfigs = new int[1];
244+
if (!EGL14.eglChooseConfig(mEGLDisplay, attribList, 0, mConfigs, 0, mConfigs.length,
245+
numConfigs, 0)) {
246+
throw new RuntimeException("unable to find RGB1010102 recordable ES2 EGL config");
247+
}
248+
249+
// Configure context for OpenGL ES 2.0.
250+
int[] attrib_list = {
251+
EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,
252+
EGL14.EGL_NONE
253+
};
254+
mEGLContext = EGL14.eglCreateContext(mEGLDisplay, mConfigs[0], EGL14.EGL_NO_CONTEXT,
255+
attrib_list, 0);
256+
checkEglError("eglCreateContext");
257+
if (mEGLContext == null) {
258+
throw new RuntimeException("null context");
259+
}
260+
261+
// Create a window surface, and attach it to the Surface we received.
262+
createEGLSurface();
263+
}
264+
240265
private void createYUVP10EGLContextAndWindow() {
241266
String extensions = EGL14.eglQueryString(mEGLDisplay, EGL14.EGL_EXTENSIONS);
242267
if (TextUtils.isEmpty(extensions) || !extensions.contains(GLUtils.EGL_YUV_EXT_NAME)) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.demo.mediacodec.transcode;
2+
3+
import com.demo.mediacodec.MediaCodecUtils;
4+
5+
/**
6+
* @author : chenqiao
7+
* @date : 2023/6/2 10:11
8+
*/
9+
public class NoSupportMediaCodecException extends Exception {
10+
11+
private final MediaCodecUtils.OutputLevel outputLevel;
12+
13+
public NoSupportMediaCodecException(String msg, MediaCodecUtils.OutputLevel outputLevel) {
14+
super(msg);
15+
this.outputLevel = outputLevel;
16+
}
17+
18+
public NoSupportMediaCodecException(String message, Throwable cause,
19+
MediaCodecUtils.OutputLevel outputLevel) {
20+
super(message, cause);
21+
this.outputLevel = outputLevel;
22+
}
23+
24+
public NoSupportMediaCodecException(Throwable cause, MediaCodecUtils.OutputLevel outputLevel) {
25+
super(cause);
26+
this.outputLevel = outputLevel;
27+
}
28+
29+
public MediaCodecUtils.OutputLevel getOutputLevel() {
30+
return outputLevel;
31+
}
32+
}

app/src/main/java/com/demo/mediacodec/transcode/TranscodeActivity.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public class TranscodeActivity extends BaseActivity implements TranscodeRunner.OnTranscodeListener {
3333

3434
private TextView mVideoInfoTv, mErrorTv;
35-
private TextInputEditText mDstWidthEdt, mDstHeightEdt, mDstBitrateEdt;
35+
private TextInputEditText mDstWidthEdt, mDstHeightEdt, mDstBitrateEdt, mDstFpsEdt;
3636
private Button mTransCodeBtn;
3737

3838
private TranscodeRunner transcodeRunner;
@@ -55,6 +55,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5555
mDstWidthEdt = findViewById(R.id.edt_dst_width);
5656
mDstHeightEdt = findViewById(R.id.edt_dst_height);
5757
mDstBitrateEdt = findViewById(R.id.edt_dst_bitrate);
58+
mDstFpsEdt = findViewById(R.id.edt_dst_fps);
5859

5960
TextWatcher watcher = new TextWatcher() {
6061
@Override
@@ -87,6 +88,7 @@ public void afterTextChanged(Editable s) {
8788
config.outWidth = Integer.parseInt(mDstWidthEdt.getEditableText().toString());
8889
config.outHeight = Integer.parseInt(mDstHeightEdt.getEditableText().toString());
8990
config.bitrate = Integer.parseInt(mDstBitrateEdt.getEditableText().toString());
91+
config.fps = Integer.parseInt(mDstFpsEdt.getEditableText().toString());
9092
try {
9193
if (config.dstPath.exists()) {
9294
config.dstPath.delete();
@@ -114,10 +116,6 @@ public void onPrepareDone(MediaFormat videoFormat) {
114116
int width = videoFormat.getInteger(MediaFormat.KEY_WIDTH);
115117
int height = videoFormat.getInteger(MediaFormat.KEY_HEIGHT);
116118
int rotation = 0;
117-
int bitrate = 3000000;
118-
if (videoFormat.containsKey(MediaFormat.KEY_BIT_RATE)) {
119-
bitrate = videoFormat.getInteger(MediaFormat.KEY_BIT_RATE);
120-
}
121119
if (videoFormat.containsKey(MediaFormat.KEY_ROTATION)) {
122120
rotation = videoFormat.getInteger(MediaFormat.KEY_ROTATION);
123121
}
@@ -128,7 +126,6 @@ public void onPrepareDone(MediaFormat videoFormat) {
128126
}
129127
mDstWidthEdt.setText(String.valueOf(width));
130128
mDstHeightEdt.setText(String.valueOf(height));
131-
mDstBitrateEdt.setText(String.valueOf(bitrate));
132129
});
133130
}
134131

0 commit comments

Comments
 (0)