Skip to content

Commit 2dbe2f1

Browse files
committed
增加对某些竖屏拍摄的视频却没有写rotation视频的兼容
1 parent d78127a commit 2dbe2f1

3 files changed

Lines changed: 46 additions & 5 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static String findDecoderByFormat(MediaFormat mediaFormat) {
4141

4242
@Nullable
4343
public static String findDecoderByFormat(MediaFormat mediaFormat, boolean onlySoftware) {
44-
MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
44+
MediaCodecList codecList = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
4545
if (!onlySoftware) {
4646
return codecList.findDecoderForFormat(mediaFormat);
4747
} else {
@@ -74,7 +74,7 @@ public static String findEncoderByFormat(MediaFormat mediaFormat) {
7474

7575
@Nullable
7676
public static String findEncoderByFormat(MediaFormat mediaFormat, boolean onlySoftware) {
77-
MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
77+
MediaCodecList codecList = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
7878
if (!onlySoftware) {
7979
return codecList.findEncoderForFormat(mediaFormat);
8080
} else {

app/src/main/java/com/demo/mediacodec/decode/DecodePlayActivity.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ private void prepareDecoder(StringBuilder log) {
157157
if (mVideoFormat == null) {
158158
return;
159159
}
160+
boolean maybeSwitchWH = false;
160161

161162
String mime = mVideoFormat.getString(MediaFormat.KEY_MIME);
162163
int width = mVideoFormat.getInteger(MediaFormat.KEY_WIDTH);
@@ -166,6 +167,9 @@ private void prepareDecoder(StringBuilder log) {
166167
rotation = mVideoFormat.getInteger(MediaFormat.KEY_ROTATION);
167168
} else {
168169
rotation = 0;
170+
if (width < height) {
171+
maybeSwitchWH = true;
172+
}
169173
}
170174
int maxCache;
171175
if (mVideoFormat.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
@@ -190,6 +194,8 @@ public void run() {
190194

191195
String codecName = MediaCodecUtils.findDecoderByFormat(mVideoFormat);
192196
if (TextUtils.isEmpty(codecName)) {
197+
log.append("prepareDecoder: 完整format没有找到解码器!\n");
198+
log.append("prepareDecoder: 尝试降级!\n");
193199
if (MediaFormat.MIMETYPE_VIDEO_DOLBY_VISION.equals(mime)) {
194200
//如果是杜比视界,那么尝试用HEVC的解码器去解
195201
mVideoFormat.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_VIDEO_HEVC);
@@ -201,15 +207,28 @@ public void run() {
201207
codecName = MediaCodecUtils.findDecoderByFormat(mVideoFormat);
202208
} else if (MediaFormat.MIMETYPE_VIDEO_HEVC.equals(mime)) {
203209
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
210+
log.append("prepareDecoder: 移除profile和level\n");
204211
//HEVC的话,尝试移除Profile和Level
205212
mVideoFormat.removeKey(MediaFormat.KEY_PROFILE);
206213
mVideoFormat.removeKey(MediaFormat.KEY_LEVEL);
207214
}
208215
codecName = MediaCodecUtils.findDecoderByFormat(mVideoFormat);
216+
if (TextUtils.isEmpty(codecName)) {
217+
log.append("prepareDecoder: 移除profile、level后format没有找到解码器!:").append(mVideoFormat).append("\n");
218+
if (maybeSwitchWH) {
219+
//Oppo有某些SB的设备,竖屏拍摄的视频,不写rotation到metadata中去,导致这里因为解码器的宽高限制,无法获取到解码器.
220+
log.append("prepareDecoder: 尝试交换Width和Height\n");
221+
MediaFormat simpleFormat = MediaFormat.createVideoFormat(mime, height, width);
222+
codecName = MediaCodecUtils.findDecoderByFormat(simpleFormat);
223+
if (TextUtils.isEmpty(codecName)) {
224+
log.append("prepareDecoder: 交换width、height也没有找到解码器!").append(simpleFormat).append("\n");
225+
}
226+
}
227+
}
209228
}
210229
}
211230
if (TextUtils.isEmpty(codecName)) {
212-
log.append("没有找到解码器!").append("\n");
231+
log.append("最终没有找到解码器!").append("\n");
213232
setDebugLog(log.toString());
214233
return;
215234
}
@@ -281,9 +300,11 @@ public void run() {
281300
mMediaExtractor.release();
282301
}
283302
log.append("解码完成,释放资源!").append("\n");
284-
setDebugLog(log.toString());
285-
} catch (IOException e) {
303+
} catch (Exception e) {
286304
e.printStackTrace();
305+
log.append("解码过程报错:" + e.getMessage());
306+
} finally {
307+
setDebugLog(log.toString());
287308
}
288309
}
289310

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ public interface OnTranscodeListener {
6161

6262
private String mOriVideoMime;
6363
private int mOriVideoWidth, mOriVideoHeight;
64+
private int mOriVideoRotation;
6465
private int mOriVideoFps;
6566
private long mVideoDurationUs;
6667

68+
private boolean mMaybeSwitchWH;
69+
6770
private OnTranscodeListener listener;
6871

6972
//媒体提取器
@@ -266,6 +269,15 @@ private void _getOriVideoInfo() {
266269
mOriVideoWidth = mOriVideoFormat.getInteger(MediaFormat.KEY_WIDTH);
267270
mOriVideoHeight = mOriVideoFormat.getInteger(MediaFormat.KEY_HEIGHT);
268271
mOriVideoFps = mOriVideoFormat.getInteger(MediaFormat.KEY_FRAME_RATE);
272+
mMaybeSwitchWH = false;
273+
if (mOriVideoFormat.containsKey(MediaFormat.KEY_ROTATION)) {
274+
mOriVideoRotation = mOriVideoFormat.getInteger(MediaFormat.KEY_ROTATION);
275+
} else {
276+
mOriVideoRotation = 0;
277+
if (mOriVideoWidth < mOriVideoHeight) {
278+
mMaybeSwitchWH = true;
279+
}
280+
}
269281
mVideoDurationUs = mOriVideoFormat.getLong(MediaFormat.KEY_DURATION);
270282
}
271283

@@ -387,6 +399,14 @@ private void prepareDecoder(VideoOutputConfig outputConfig) throws Exception {
387399
mOriVideoFormat.removeKey(MediaFormat.KEY_LEVEL);
388400
}
389401
codecName = MediaCodecUtils.findDecoderByFormat(mOriVideoFormat);
402+
403+
if (TextUtils.isEmpty(codecName)) {
404+
if (mMaybeSwitchWH) {
405+
//Oppo有某些SB的设备,竖屏拍摄的视频,不写rotation到metadata中去,导致这里因为解码器的宽高限制,无法获取到解码器.
406+
MediaFormat simpleFormat = MediaFormat.createVideoFormat(mOriVideoMime, mOriVideoHeight, mOriVideoWidth);
407+
codecName = MediaCodecUtils.findDecoderByFormat(simpleFormat);
408+
}
409+
}
390410
} else {
391411
throw new RuntimeException("没有找到合适的解码器! videoFormat:" + mOriVideoFormat);
392412
}

0 commit comments

Comments
 (0)