diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/video/MediaCodecVideoRenderer.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/video/MediaCodecVideoRenderer.java index 247e54fdf8..bf74a4b8ed 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/video/MediaCodecVideoRenderer.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/video/MediaCodecVideoRenderer.java @@ -1342,12 +1342,16 @@ protected void onOutputFormatChanged(Format format, @Nullable MediaFormat mediaF ? mediaFormat.getInteger(KEY_CROP_BOTTOM) - mediaFormat.getInteger(KEY_CROP_TOP) + 1 : mediaFormat.getInteger(MediaFormat.KEY_HEIGHT); } - boolean hasPixelAspectRatio = (Util.SDK_INT >= 30) && mediaFormat != null && mediaFormat.containsKey(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH) - && mediaFormat.containsKey(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT); - pixelWidthHeightRatio = hasPixelAspectRatio ? - (float)mediaFormat.getInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH) / - mediaFormat.getInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT) - : format.pixelWidthHeightRatio; + boolean hasPixelAspectRatio = + (Util.SDK_INT >= 30) + && mediaFormat != null + && mediaFormat.containsKey(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH) + && mediaFormat.containsKey(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT); + pixelWidthHeightRatio = + hasPixelAspectRatio + ? (float) mediaFormat.getInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH) + / mediaFormat.getInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT) + : format.pixelWidthHeightRatio; // The decoder applies the rotation when rendering to the surface. For 90 and 270 degree // rotations, we need to flip the width, height and pixel aspect ratio to reflect the rotation // that was applied. diff --git a/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/video/MediaCodecVideoRendererTest.java b/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/video/MediaCodecVideoRendererTest.java index 203dd3fcc3..fa084d8e10 100644 --- a/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/video/MediaCodecVideoRendererTest.java +++ b/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/video/MediaCodecVideoRendererTest.java @@ -751,11 +751,27 @@ public void render_sendsVideoSizeChangeWithCurrentFormatValues() throws Exceptio @Config(minSdk = 30) @Test - public void render_withMediaCodecAlteringPixelAspectRatioWidthHeight_sendsVideoSizeChangeWithMediaFormatValues() throws Exception { + public void + render_withMediaCodecModifyingPixelAspectRatioWidthHeight_sendsVideoSizeChangeWithMediaFormatValues() + throws Exception { MediaCodecAdapter.Factory codecAdapterFactory = configuration -> - new ForwardingSynchronousMediaCodecAdapterAlteringPixelAspectRatio( - new SynchronousMediaCodecAdapter.Factory().createAdapter(configuration)); + new ForwardingSynchronousMediaCodecAdapter( + new SynchronousMediaCodecAdapter.Factory().createAdapter(configuration)) { + @Override + public MediaFormat getOutputFormat() { + MediaFormat mediaFormat = adapter.getOutputFormat(); + if (Util.SDK_INT >= 30) { + int pixelAspectRatioHeight = 1 << 30; // Max integer power of 2. + int pixelAspectRatioWidth = (int) (0.5f * pixelAspectRatioHeight); + mediaFormat.setInteger( + MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH, pixelAspectRatioWidth); + mediaFormat.setInteger( + MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT, pixelAspectRatioHeight); + } + return mediaFormat; + } + }; MediaCodecVideoRenderer mediaCodecVideoRendererWithCustomAdapter = new MediaCodecVideoRenderer( ApplicationProvider.getApplicationContext(), @@ -773,9 +789,8 @@ public void render_withMediaCodecAlteringPixelAspectRatioWidthHeight_sendsVideoS } }; mediaCodecVideoRendererWithCustomAdapter.init(/* index= */ 0, PlayerId.UNSET, Clock.DEFAULT); - surface = new Surface(new SurfaceTexture(/* texName= */ 0)); - mediaCodecVideoRendererWithCustomAdapter.handleMessage(Renderer.MSG_SET_VIDEO_OUTPUT, surface); - + mediaCodecVideoRendererWithCustomAdapter.handleMessage( + Renderer.MSG_SET_VIDEO_OUTPUT, new Surface(new SurfaceTexture(/* texName= */ 0))); FakeSampleStream fakeSampleStream = new FakeSampleStream( new DefaultAllocator(/* trimOnReset= */ true, /* individualAllocationSize= */ 1024), @@ -801,7 +816,8 @@ public void render_withMediaCodecAlteringPixelAspectRatioWidthHeight_sendsVideoS int positionUs = 0; do { - mediaCodecVideoRendererWithCustomAdapter.render(positionUs, SystemClock.elapsedRealtime() * 1000); + mediaCodecVideoRendererWithCustomAdapter.render( + positionUs, SystemClock.elapsedRealtime() * 1000); positionUs += 10; } while (!mediaCodecVideoRendererWithCustomAdapter.isEnded()); shadowOf(testMainLooper).idle(); @@ -809,9 +825,7 @@ public void render_withMediaCodecAlteringPixelAspectRatioWidthHeight_sendsVideoS verify(eventListener) .onVideoSizeChanged( new VideoSize( - VIDEO_H264.width, - VIDEO_H264.height, - VIDEO_H264.pixelWidthHeightRatio / 2)); + VIDEO_H264.width, VIDEO_H264.height, VIDEO_H264.pixelWidthHeightRatio / 2)); } @Test @@ -1968,28 +1982,6 @@ public int dequeueOutputBufferIndex(MediaCodec.BufferInfo bufferInfo) { } } - private static final class ForwardingSynchronousMediaCodecAdapterAlteringPixelAspectRatio - extends ForwardingSynchronousMediaCodecAdapter { - - ForwardingSynchronousMediaCodecAdapterAlteringPixelAspectRatio(MediaCodecAdapter adapter) { - super(adapter); - } - - @Override - public MediaFormat getOutputFormat() { - MediaFormat mediaFormat = adapter.getOutputFormat(); - if (Util.SDK_INT >= 30) { - int pixelAspectRatioHeight = 1 << 30; - int pixelAspectRatioWidth = (int) (0.5f * pixelAspectRatioHeight); - mediaFormat.setInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH, - pixelAspectRatioWidth); - mediaFormat.setInteger(MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT, - pixelAspectRatioHeight); - } - return mediaFormat; - } - } - private abstract static class ForwardingSynchronousMediaCodecAdapter implements MediaCodecAdapter { protected final MediaCodecAdapter adapter;