Skip to content

Commit c6b4081

Browse files
committed
修复杜比视界转码后颜色不对的问题
1 parent a4399f0 commit c6b4081

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

.DS_Store

6 KB
Binary file not shown.

app/.DS_Store

6 KB
Binary file not shown.

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ private void prepareEncoder(VideoOutputConfig outputConfig) throws Exception {
303303
throw new NoSupportMediaCodecException("没有找到合适的编码器! outputFormat:" + mOutputFormat,
304304
outputConfig.outputLevel);
305305
}
306+
Log.i("TranscodeRunner", "使用编码器" +
307+
": " + codecName);
306308
mEncodeCodecThread = new HandlerThread("EncodeCodecThread");
307309
mEncodeCodecThread.start();
308310
mEncodeCodecHandler = new Handler(mEncodeCodecThread.getLooper());
@@ -391,9 +393,11 @@ public void onOutputFormatChanged(@NonNull MediaCodec codec,
391393
private void prepareDecoder(VideoOutputConfig outputConfig) throws Exception {
392394
decodeFrameIndex = 0;
393395
encodeFrameIndex = 0;
396+
boolean isDolby = MediaFormat.MIMETYPE_VIDEO_DOLBY_VISION.equals(mOriVideoMime);
397+
boolean useDolbyDec = false;
394398
String codecName = MediaCodecUtils.findDecoderByFormat(mOriVideoFormat);
395399
if (TextUtils.isEmpty(codecName)) {
396-
if (MediaFormat.MIMETYPE_VIDEO_DOLBY_VISION.equals(mOriVideoMime)) {
400+
if (isDolby) {
397401
//如果是杜比视界,那么尝试用HEVC的解码器去解
398402
mOriVideoFormat.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_VIDEO_HEVC);
399403
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
@@ -421,10 +425,15 @@ private void prepareDecoder(VideoOutputConfig outputConfig) throws Exception {
421425
} else {
422426
throw new RuntimeException("没有找到合适的解码器! videoFormat:" + mOriVideoFormat);
423427
}
428+
} else {
429+
if (isDolby) {
430+
useDolbyDec = true;
431+
}
424432
}
425433
if (TextUtils.isEmpty(codecName)) {
426434
throw new RuntimeException("没有找到合适的解码器! videoFormat:" + mOriVideoFormat);
427435
}
436+
Log.i("TranscodeRunner", "使用解码器: " + codecName);
428437
mDecodeCodecThread = new HandlerThread("DecodeCodecThread");
429438
mDecodeCodecThread.start();
430439
mDecodeCodecHandler = new Handler(mDecodeCodecThread.getLooper());
@@ -577,6 +586,12 @@ public void onOutputFormatChanged(@NonNull MediaCodec codec,
577586
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
578587
mOriVideoFormat.setInteger("allow-frame-drop", 0);
579588
}
589+
if (isDolby && useDolbyDec) {
590+
Bundle transferBundle = new Bundle();
591+
String value = "transfer.hlg"; //还有一种是"transfer.dolby"
592+
transferBundle.putString("vendor.dolby.codec.transfer.value", value);
593+
mDecoder.setParameters(transferBundle);
594+
}
580595
mDecoder.configure(mOriVideoFormat, mDecoderOutputSurface.getSurface(), null, 0);
581596
}
582597

0 commit comments

Comments
 (0)