Skip to content

Commit 80da2cf

Browse files
authored
[video_player_android] Report display size for anamorphic video (#12360)
`VideoSize.pixelWidthHeightRatio` was never read, so videos stored with non-square pixels (anamorphic content) reported their **coded** size instead of their **display** size. `VideoPlayerValue.size` therefore carried the wrong aspect ratio and such videos rendered stretched. Example of a real affected file: coded `1080x720` with a `3:8` pixel aspect ratio displays as `405x720`, but `size` was reported as `1080x720` — a 2.67x horizontal stretch. This scales the reported width by the pixel aspect ratio, which is what media3's own [`PlayerView`](https://github.com/androidx/media/blob/release/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java) does: ```java float videoAspectRatio = (height == 0 || width == 0) ? 0 : (width * videoSize.pixelWidthHeightRatio) / height; ``` Both event listeners are updated: - **Texture path** — ExoPlayer reports a `pixelWidthHeightRatio` that already accounts for any applied rotation (`MediaCodecVideoRenderer` inverts it for 90°/270°), so the ratio is applied to the reported width regardless of the rotation correction. - **Platform view path** — `Format.pixelWidthHeightRatio` describes the unrotated frame, so the ratio is applied *before* the existing width/height swap. The scaling is skipped unless the ratio is greater than 0 and not 1, so playback of ordinary square-pixel video is byte-for-byte unchanged. Verified on a physical anamorphic sample (`1080x720`, SAR `3:8`) on Android 15: previously stretched, now rendered at the correct 9:16 display aspect. iOS is unaffected — `video_player_avfoundation` already reports `presentationSize`, which is pixel-aspect corrected, which is why this bug is Android-only. *(No before/after screenshots: the sample videos available to me are private medical content and cannot be published.)* Fixes flutter/flutter#132934 Fixes flutter/flutter#94234 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 18fe786 commit 80da2cf

6 files changed

Lines changed: 145 additions & 3 deletions

File tree

‎packages/video_player/video_player_android/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.12.2
2+
3+
* Fixes a [bug](https://github.com/flutter/flutter/issues/132934) where videos with a pixel aspect ratio other than 1.0 (anamorphic content) reported their coded size instead of their display size, causing them to be rendered stretched.
4+
15
## 2.12.1
26

37
* Updates pigeon dev_dependency to ^27.3.2 for analyzer 14 compatibility.

‎packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/platformview/PlatformViewExoPlayerEventListener.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,23 @@ protected void sendInitialized() {
3030
int width = videoFormat.width;
3131
int height = videoFormat.height;
3232

33+
// Anamorphic content is stored with non-square pixels, so the coded width has to be
34+
// scaled by the pixel aspect ratio to obtain the display width. This is applied before
35+
// the rotation swap below, because Format reports the ratio for the unrotated frame.
36+
// Width is only scaled when it is known (Format.NO_VALUE is negative), and is clamped to at
37+
// least one pixel so that a malformed ratio cannot report a zero width.
38+
float pixelWidthHeightRatio = videoFormat.pixelWidthHeightRatio;
39+
if (width > 0 && pixelWidthHeightRatio > 0 && pixelWidthHeightRatio != 1f) {
40+
width = Math.max(1, Math.round(width * pixelWidthHeightRatio));
41+
}
42+
3343
// Switch the width/height if video was taken in portrait mode and a rotation
3444
// correction was detected.
3545
if (rotationCorrection == RotationDegrees.ROTATE_90
3646
|| rotationCorrection == RotationDegrees.ROTATE_270) {
37-
width = videoFormat.height;
38-
height = videoFormat.width;
47+
int displayWidth = width;
48+
width = height;
49+
height = displayWidth;
3950

4051
rotationCorrection = RotationDegrees.fromDegrees(0);
4152
}

‎packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/texture/TextureExoPlayerEventListener.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ protected void sendInitialized() {
3131
int width = videoSize.width;
3232
int height = videoSize.height;
3333
if (width != 0 && height != 0) {
34+
// Anamorphic content is stored with non-square pixels, so the coded width has to be
35+
// scaled by the pixel aspect ratio to obtain the display width. ExoPlayer reports a
36+
// pixelWidthHeightRatio that already accounts for any applied rotation, so this is
37+
// correct regardless of the rotation correction computed below.
38+
// Clamped to at least one pixel so that a malformed ratio cannot report a zero width.
39+
float pixelWidthHeightRatio = videoSize.pixelWidthHeightRatio;
40+
if (pixelWidthHeightRatio > 0 && pixelWidthHeightRatio != 1f) {
41+
width = Math.max(1, Math.round(width * pixelWidthHeightRatio));
42+
}
43+
3444
// When the SurfaceTexture backend for Impeller is used, the preview should already
3545
// be correctly rotated.
3646
if (!surfaceProducerHandlesCropAndRotation) {

‎packages/video_player/video_player_android/android/src/test/java/io/flutter/plugins/videoplayer/PlatformViewExoPlayerEventListenerTest.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,64 @@ public void onPlaybackStateChangedReadyInPortraitMode270DegreesSwapsWidthAndHeig
7777
eventListener.onPlaybackStateChanged(Player.STATE_READY);
7878
verify(mockCallbacks).onInitialized(400, 800, 10L, 0);
7979
}
80+
81+
@Test
82+
public void onPlaybackStateChangedReadyScalesWidthByPixelAspectRatio_whenContentIsAnamorphic() {
83+
eventListener = new PlatformViewExoPlayerEventListener(mockExoPlayer, mockCallbacks);
84+
85+
// Anamorphic content: 1080x720 coded pixels with a 3:8 pixel aspect ratio displays as 405x720.
86+
Format format =
87+
new Format.Builder().setWidth(1080).setHeight(720).setPixelWidthHeightRatio(0.375f).build();
88+
when(mockExoPlayer.getVideoFormat()).thenReturn(format);
89+
when(mockExoPlayer.getDuration()).thenReturn(10L);
90+
91+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
92+
verify(mockCallbacks).onInitialized(405, 720, 10L, 0);
93+
}
94+
95+
@Test
96+
public void onPlaybackStateChangedReadyScalesThenSwaps_whenAnamorphicAndRotated90Degrees() {
97+
eventListener = new PlatformViewExoPlayerEventListener(mockExoPlayer, mockCallbacks);
98+
99+
// The display size of the unrotated frame is 405x720, so rotating it yields 720x405.
100+
Format format =
101+
new Format.Builder()
102+
.setWidth(1080)
103+
.setHeight(720)
104+
.setPixelWidthHeightRatio(0.375f)
105+
.setRotationDegrees(90)
106+
.build();
107+
when(mockExoPlayer.getVideoFormat()).thenReturn(format);
108+
when(mockExoPlayer.getDuration()).thenReturn(10L);
109+
110+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
111+
verify(mockCallbacks).onInitialized(720, 405, 10L, 0);
112+
}
113+
114+
@Test
115+
public void onPlaybackStateChangedReadyReportsAtLeastOnePixel_whenPixelAspectRatioIsDegenerate() {
116+
eventListener = new PlatformViewExoPlayerEventListener(mockExoPlayer, mockCallbacks);
117+
118+
// A malformed ratio would otherwise round the width down to zero.
119+
Format format =
120+
new Format.Builder().setWidth(800).setHeight(400).setPixelWidthHeightRatio(0.0001f).build();
121+
when(mockExoPlayer.getVideoFormat()).thenReturn(format);
122+
when(mockExoPlayer.getDuration()).thenReturn(10L);
123+
124+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
125+
verify(mockCallbacks).onInitialized(1, 400, 10L, 0);
126+
}
127+
128+
@Test
129+
public void onPlaybackStateChangedReadyLeavesUnknownWidthUnscaled() {
130+
eventListener = new PlatformViewExoPlayerEventListener(mockExoPlayer, mockCallbacks);
131+
132+
// Format.NO_VALUE must not be turned into a bogus scaled width.
133+
Format format = new Format.Builder().setHeight(400).setPixelWidthHeightRatio(0.375f).build();
134+
when(mockExoPlayer.getVideoFormat()).thenReturn(format);
135+
when(mockExoPlayer.getDuration()).thenReturn(10L);
136+
137+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
138+
verify(mockCallbacks).onInitialized(Format.NO_VALUE, 400, 10L, 0);
139+
}
80140
}

‎packages/video_player/video_player_android/android/src/test/java/io/flutter/plugins/videoplayer/TextureExoPlayerEventListenerTest.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,61 @@ public class TextureExoPlayerEventListenerTest {
124124
eventListener.onPlaybackStateChanged(Player.STATE_READY);
125125
verify(mockCallbacks).onInitialized(800, 400, 10L, 270);
126126
}
127+
128+
@Test
129+
public void onPlaybackStateChangedReadyScalesWidthByPixelAspectRatio_whenContentIsAnamorphic() {
130+
TextureExoPlayerEventListener eventListener =
131+
new TextureExoPlayerEventListener(mockExoPlayer, mockCallbacks, true);
132+
// Anamorphic content: 1080x720 coded pixels with a 3:8 pixel aspect ratio displays as 405x720.
133+
VideoSize size = new VideoSize(1080, 720, 0.375f);
134+
when(mockExoPlayer.getVideoSize()).thenReturn(size);
135+
when(mockExoPlayer.getDuration()).thenReturn(10L);
136+
137+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
138+
verify(mockCallbacks).onInitialized(405, 720, 10L, 0);
139+
}
140+
141+
@Test
142+
public void onPlaybackStateChangedReadyDoesNotScaleWidth_whenPixelAspectRatioIsSquare() {
143+
TextureExoPlayerEventListener eventListener =
144+
new TextureExoPlayerEventListener(mockExoPlayer, mockCallbacks, true);
145+
VideoSize size = new VideoSize(800, 400, 1f);
146+
when(mockExoPlayer.getVideoSize()).thenReturn(size);
147+
when(mockExoPlayer.getDuration()).thenReturn(10L);
148+
149+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
150+
verify(mockCallbacks).onInitialized(800, 400, 10L, 0);
151+
}
152+
153+
@Test
154+
public void
155+
onPlaybackStateChangedReadyScalesWidthByPixelAspectRatio_whenAnamorphicAndRotationCorrected() {
156+
TextureExoPlayerEventListener eventListener =
157+
new TextureExoPlayerEventListener(mockExoPlayer, mockCallbacks, false);
158+
// ExoPlayer reports a pixel aspect ratio that already accounts for the applied rotation,
159+
// so the width is scaled the same way regardless of the rotation correction.
160+
VideoSize size = new VideoSize(1080, 720, 0.375f);
161+
int rotationCorrection = 90;
162+
Format videoFormat = new Format.Builder().setRotationDegrees(rotationCorrection).build();
163+
164+
when(mockExoPlayer.getVideoSize()).thenReturn(size);
165+
when(mockExoPlayer.getDuration()).thenReturn(10L);
166+
when(mockExoPlayer.getVideoFormat()).thenReturn(videoFormat);
167+
168+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
169+
verify(mockCallbacks).onInitialized(405, 720, 10L, rotationCorrection);
170+
}
171+
172+
@Test
173+
public void onPlaybackStateChangedReadyReportsAtLeastOnePixel_whenPixelAspectRatioIsDegenerate() {
174+
TextureExoPlayerEventListener eventListener =
175+
new TextureExoPlayerEventListener(mockExoPlayer, mockCallbacks, true);
176+
// A malformed ratio would otherwise round the width down to zero.
177+
VideoSize size = new VideoSize(800, 400, 0.0001f);
178+
when(mockExoPlayer.getVideoSize()).thenReturn(size);
179+
when(mockExoPlayer.getDuration()).thenReturn(10L);
180+
181+
eventListener.onPlaybackStateChanged(Player.STATE_READY);
182+
verify(mockCallbacks).onInitialized(1, 400, 10L, 0);
183+
}
127184
}

‎packages/video_player/video_player_android/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: video_player_android
22
description: Android implementation of the video_player plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/video_player/video_player_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
5-
version: 2.12.1
5+
version: 2.12.2
66

77
environment:
88
sdk: ^3.12.0

0 commit comments

Comments
 (0)