HDR 视频播放

HDR 即高动态范围 (HDR),它提供的颜色范围更广泛,最亮白色和最暗阴影之间的对比度更高,因此视频质量更接近肉眼的肉眼感知。

您可以在应用中设置 HDR 视频播放,以预览和播放 HDR 视频内容。

本文假定您已为应用添加了基本的视频播放支持。如需详细了解播放,请参阅 ExoPlayer 文档。

设备前提条件

并非所有 Android 设备都支持 HDR 播放。在应用中播放 HDR 视频内容之前,请先确定您的设备是否满足以下前提条件:

  • 以 Android 7.0 或更高版本(API 层 24)为目标平台。
  • 具有支持 HDR 的解码器,并可使用支持 HDR 的显示屏。

检查是否支持 HDR 播放

使用 Display.getHdrCapabilities() 查询显示屏的 HDR capability。该方法会返回有关支持的 HDR 配置文件和显示屏的亮度范围的信息。

以下代码会检查设备是否支持 HLG10 播放。从 Android 13 开始,如果设备能够进行 HDR 播放,HLG10 将成为设备制造商必须支持的最低标准:

Kotlin

// Check if display supports the HDR type
val capabilities = display?.hdrCapabilities?.supportedHdrTypes ?: intArrayOf()
if (!capabilities.contains(HDR_TYPE_HLG)) {
  throw RuntimeException("Display does not support desired HDR type");
}

Java

// Check if display supports the HDR type
int[] list = getDisplay().getHdrCapabilities().getSupportedHdrTypes();
List capabilities = Arrays.stream(list).boxed().collect(Collectors.toList());
if (!capabilities.contains(HDR_TYPE_HLG)) {
 throw new RuntimeException("Display does not support desired HDR type");
}

在您的应用中设置 HDR 播放

如果您的应用使用 ExoPlayer,默认情况下支持 HDR 播放。请参阅检查是否支持 HDR 播放,了解后续步骤。

如果您的应用不使用 ExoPlayer,请通过 SurfaceView 使用 MediaCodec 设置 HDR 播放。

使用 SurfaceView 设置 MediaCodec

使用 SurfaceView 设置标准 MediaCodec 播放流程。这样一来,您无需对 HDR 播放进行任何特殊处理即可显示 HDR 视频内容:

  • MediaCodec:对 HDR 视频内容进行解码。
  • SurfaceView:显示 HDR 视频内容。

以下代码会检查编解码器是否支持 HDR 配置,然后使用 SurfaceView 设置 MediaCodec

Kotlin

// Check if there's a codec that supports the specific HDR profile
val list = MediaCodecList(MediaCodecList.REGULAR_CODECS) var format = MediaFormat() /* media format from the container */;
format.setInteger(MediaFormat.KEY_PROFILE, MediaCodecInfo.CodecProfileLevel.AV1ProfileMain10)
val codecName = list.findDecoderForFormat (format) ?: throw RuntimeException ("No codec supports the format")

// Here is a standard MediaCodec playback flow
val codec: MediaCodec = MediaCodec.createByCodecName(codecName);
val surface: Surface = surfaceView.holder.surface
val callback: MediaCodec.Callback = (object : MediaCodec.Callback() {
   override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {
      queue.offer(index)
   }

   override fun onOutputBufferAvailable(
      codec: MediaCodec,
      index: Int,
      info: MediaCodec.BufferInfo
   ) {
      codec.releaseOutputBuffer(index, timestamp)
   }

   override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) {
      // handle error
   }

   override fun onOutputFormatChanged(
      codec: MediaCodec, format: MediaFormat
   ) {
      // handle format change
   }
})

codec.setCallback(callback)
codec.configure(format, surface, crypto, 0 /* flags */)
codec.start()
while (/* until EOS */) {
   val index = queue.poll()
   val buffer = codec.getInputBuffer(index)
   buffer?.put(/* write bitstream */)
   codec.queueInputBuffer(index, offset, size, timestamp, flags)
}
codec.stop()
codec.release()

Java

// Check if there's a codec that supports the specific HDR profile
MediaCodecList list = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
MediaFormat format = /* media format from the container */;
format.setInteger(
    MediaFormat.KEY_PROFILE, CodecProfileLevel.AV1ProfileMain10);
String codecName = list.findDecoderForFormat(format);
if (codecName == null) {
    throw new RuntimeException("No codec supports the format");
}

// Below is a standard MediaCodec playback flow
MediaCodec codec = MediaCodec.getCodecByName(codecName);
Surface surface = surfaceView.getHolder().getSurface();
MediaCodec.Callback callback = new MediaCodec.Callback() {
    @Override
    void onInputBufferAvailable(MediaCodec codec, int index) {
        queue.offer(index);
    }

    @Override
    void onOutputBufferAvailable(MediaCodec codec, int index) {
        // release the buffer for render
        codec.releaseOutputBuffer(index, timestamp);
    }

    @Override
    void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
        // handle format change
    }

    @Override
    void onError(MediaCodec codec, MediaCodec.CodecException ex) {
        // handle error
    }

};
codec.setCallback(callback);
codec.configure(format, surface, crypto, 0 /* flags */);
codec.start();
while (/* until EOS */) {
    int index = queue.poll();
    ByteBuffer buffer = codec.getInputBuffer(index);
    buffer.put(/* write bitstream */);
    codec.queueInputBuffer(index, offset, size, timestamp, flags);
}
codec.stop();
codec.release();

如需了解更多使用 SurfaceViewMediaCodec 实现,请参阅 Android 相机示例

资源

如需详细了解 HDR 播放,请参阅以下资源:

HDR

媒体