Pemutaran video HDR

HDR, atau High Dynamic Range, memberikan rentang warna yang lebih luas dan kontras yang lebih besar antara warna putih paling terang dan bayangan tergelap, sehingga menghasilkan kualitas video yang lebih mirip dengan apa yang terlihat oleh mata telanjang.

Anda dapat menyiapkan pemutaran video HDR di aplikasi untuk melihat pratinjau dan memutar konten video HDR.

Artikel ini mengasumsikan bahwa Anda telah menambahkan dukungan pemutaran video dasar ke aplikasi. Lihat dokumentasi ExoPlayer untuk detail selengkapnya tentang pemutaran.

Prasyarat perangkat

Tidak semua perangkat Android mendukung pemutaran HDR. Sebelum memutar konten video HDR di aplikasi Anda, tentukan apakah perangkat Anda memenuhi prasyarat berikut:

  • Menargetkan Android 7.0 atau yang lebih tinggi (lapisan API 24).
  • Memiliki decoder berkemampuan HDR dan akses ke layar berkemampuan HDR.

Memeriksa dukungan pemutaran HDR

Gunakan Display.getHdrCapabilities() untuk mengkueri kemampuan HDR layar. Metode ini menampilkan informasi tentang profil HDR yang didukung dan rentang luminans untuk layar.

Kode berikut memeriksa apakah perangkat mendukung pemutaran HLG10. Mulai Android 13, HLG10 adalah standar minimum yang harus didukung oleh produsen perangkat jika perangkat tersebut dapat memutar HDR:

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");
}

Menyiapkan pemutaran HDR di aplikasi Anda

Jika aplikasi Anda menggunakan ExoPlayer, aplikasi tersebut mendukung pemutaran HDR secara default. Lihat Memeriksa dukungan pemutaran HDR untuk mengetahui langkah selanjutnya.

Jika aplikasi Anda tidak menggunakan ExoPlayer, siapkan pemutaran HDR menggunakan MediaCodec melalui SurfaceView.

Menyiapkan MediaCodec menggunakan SurfaceView

Siapkan alur pemutaran MediaCodec standar menggunakan SurfaceView. Hal ini memungkinkan Anda menampilkan konten video HDR tanpa penanganan khusus untuk pemutaran HDR:

  • MediaCodec: Mendekode konten video HDR.
  • SurfaceView: Menampilkan konten video HDR.

Kode berikut memeriksa apakah codec mendukung profil HDR, lalu menyiapkan MediaCodec menggunakan SurfaceView:

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();

Untuk implementasi MediaCodec lainnya yang menggunakan SurfaceView, lihat contoh Kamera Android.

Referensi

Untuk informasi selengkapnya terkait pemutaran HDR, lihat referensi berikut:

HDR

Media