הפעלת סרטוני HDR

‫HDR, או טווח דינמי גבוה, מספק טווח רחב יותר של צבעים וניגודיות גבוהה יותר בין הלבנים הבהירים ביותר לבין הצללים הכהים ביותר, וכתוצאה מכך איכות הסרטון דומה יותר למה שהעין רואה.

אתם יכולים להגדיר באפליקציה הפעלה של סרטוני HDR כדי לצפות בתצוגה מקדימה של תוכן וידאו ב-HDR ולהפעיל אותו.

במאמר הזה אנחנו מניחים שכבר הוספתם לאפליקציה תמיכה בסיסית בהפעלת סרטונים. לפרטים נוספים על הפעלה, אפשר לעיין במסמכי התיעוד של ExoPlayer.

דרישות מוקדמות לגבי מכשירים

לא כל מכשירי Android תומכים בהפעלה של HDR. לפני שמפעילים תוכן וידאו באיכות HDR באפליקציה, צריך לבדוק אם המכשיר עומד בדרישות המוקדמות הבאות:

  • מטרגטת ל-Android 7.0 ומעלה (שכבת API‏ 24).
  • יש לו מפענח שתומך ב-HDR וגישה למסך שתומך ב-HDR.

בדיקה אם יש תמיכה בהפעלה באיכות HDR

כדי לשאול על יכולות ה-HDR של מסך, משתמשים ב-Display.getHdrCapabilities(). השיטה מחזירה מידע על פרופילי ה-HDR הנתמכים ועל טווח הבהירות של המסך.

הקוד הבא בודק אם המכשיר תומך בהפעלה של HLG10. החל מ-Android 13, ‏ HLG10 הוא התקן המינימלי שיוצרי מכשירים צריכים לתמוך בו אם המכשיר מסוגל להפעיל תוכן ב-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");
}

הגדרת הפעלה ב-HDR באפליקציה

אם האפליקציה שלכם משתמשת ב-ExoPlayer, היא תומכת בהפעלה באיכות HDR כברירת מחדל. במאמר בדיקה אם יש תמיכה בהפעלת HDR מוסבר מה השלבים הבאים.

אם האפליקציה לא משתמשת ב-ExoPlayer, צריך להגדיר הפעלת HDR באמצעות MediaCodec דרך SurfaceView.

הגדרה של MediaCodec באמצעות SurfaceView

מגדירים תהליך הפעלה רגיל של MediaCodec באמצעות SurfaceView. כך אפשר להציג תוכן וידאו באיכות HDR בלי לבצע פעולות מיוחדות להפעלת HDR:

  • MediaCodec: מפענח תוכן וידאו ב-HDR.
  • SurfaceView: הצגת תוכן וידאו באיכות HDR.

הקוד הבא בודק אם הקודק תומך בפרופיל HDR, ואז מגדיר את MediaCodec באמצעות 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();

לדוגמאות נוספות של הטמעות באמצעות MediaCodec, אפשר לעיין בדוגמאות של מצלמות Android.SurfaceView

משאבים

מידע נוסף על הפעלת HDR זמין במקורות המידע הבאים:

HDR

מדיה

  • הפניית Media API: מידע נוסף על ממשקי Media API.
  • ExoPlayer: מידע על הגדרת האפליקציה באמצעות ספריית ExoPlayer.