From 60ce524ea3f066a04094adc4f1cc7498d20b03da Mon Sep 17 00:00:00 2001
From: vladikadiroff <vladikadiroff@gmail.com>
Date: Fri, 27 Dec 2024 01:56:17 +0300
Subject: [PATCH] add needsReconfigureDueAspectRatioChangesWorkaround check

---
 .../exoplayer/mediacodec/MediaCodecInfo.java  | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecInfo.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecInfo.java
index 2485793e556..49ffda22dc2 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecInfo.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecInfo.java
@@ -419,6 +419,9 @@ public DecoderReuseEvaluation canReuseCodec(Format oldFormat, Format newFormat)
           && !oldFormat.initializationDataEquals(newFormat)) {
         discardReasons |= DISCARD_REASON_WORKAROUND;
       }
+      if (needsReconfigureDueAspectRatioChangesWorkaround(name, oldFormat, newFormat)) {
+        discardReasons |= DISCARD_REASON_WORKAROUND;
+      }
 
       if (discardReasons == 0) {
         return new DecoderReuseEvaluation(
@@ -797,6 +800,31 @@ private static boolean needsAdaptationReconfigureWorkaround(String name) {
     return Util.MODEL.startsWith("SM-T230") && "OMX.MARVELL.VIDEO.HW.CODA7542DECODER".equals(name);
   }
 
+  /**
+   * Returns whether the decoder is known to behave incorrectly if reused
+   * when the new format has a different aspect ratio.
+   *
+   * @param name The name of the decoder.
+   * @param oldFormat The format being decoded.
+   * @param newFormat The new format.
+   * @return Whether the decoder is known to behave incorrectly if reused when the new format has
+   * a different aspect ratio.
+   */
+  private static boolean needsReconfigureDueAspectRatioChangesWorkaround(
+      String name,
+      Format oldFormat,
+      Format newFormat
+  ) {
+    // See https://github.com/androidx/media/issues/2003
+    if ("c2.exynos.h264.decoder".equals(name) || "c2.android.avc.decoder".equals(name)) {
+      float oldAspectRatio = (float) oldFormat.width / oldFormat.height;
+      float newAspectRatio = (float) newFormat.width / newFormat.height;
+      return oldAspectRatio != newAspectRatio;
+    }
+
+    return false;
+  }
+
   /**
    * Returns whether the decoder is known to behave incorrectly if flushed to adapt to a new format.
    *