From 972fd0ced85f896f3a5cce3491db96748627a334 Mon Sep 17 00:00:00 2001 From: olly Date: Tue, 17 Jul 2018 05:55:52 -0700 Subject: [PATCH] Use maxInputSize for extension decoders where possible Issue: #3120 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=204898773 --- .../ext/ffmpeg/FfmpegAudioRenderer.java | 19 ++++++------------- .../exoplayer2/ext/flac/FlacDecoder.java | 15 ++++++++++++--- .../ext/flac/LibflacAudioRenderer.java | 3 ++- .../ext/opus/LibopusAudioRenderer.java | 15 ++++++++++++--- .../ext/vp9/LibvpxVideoRenderer.java | 11 +++++------ 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegAudioRenderer.java b/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegAudioRenderer.java index bd6e7ad55c..4218af3c7f 100644 --- a/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegAudioRenderer.java +++ b/extensions/ffmpeg/src/main/java/com/google/android/exoplayer2/ext/ffmpeg/FfmpegAudioRenderer.java @@ -37,15 +37,10 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; */ public final class FfmpegAudioRenderer extends SimpleDecoderAudioRenderer { - /** - * The number of input and output buffers. - */ + /** The number of input and output buffers. */ private static final int NUM_BUFFERS = 16; - /** - * The initial input buffer size. Input buffers are reallocated dynamically if this value is - * insufficient. - */ - private static final int INITIAL_INPUT_BUFFER_SIZE = 960 * 6; + /** The default input buffer size. */ + private static final int DEFAULT_INPUT_BUFFER_SIZE = 960 * 6; private final boolean enableFloatOutput; @@ -120,13 +115,11 @@ public final class FfmpegAudioRenderer extends SimpleDecoderAudioRenderer { @Override protected FfmpegDecoder createDecoder(Format format, ExoMediaCrypto mediaCrypto) throws FfmpegDecoderException { + int initialInputBufferSize = + format.maxInputSize != Format.NO_VALUE ? format.maxInputSize : DEFAULT_INPUT_BUFFER_SIZE; decoder = new FfmpegDecoder( - NUM_BUFFERS, - NUM_BUFFERS, - INITIAL_INPUT_BUFFER_SIZE, - format, - shouldUseFloatOutput(format)); + NUM_BUFFERS, NUM_BUFFERS, initialInputBufferSize, format, shouldUseFloatOutput(format)); return decoder; } diff --git a/extensions/flac/src/main/java/com/google/android/exoplayer2/ext/flac/FlacDecoder.java b/extensions/flac/src/main/java/com/google/android/exoplayer2/ext/flac/FlacDecoder.java index e8a04e06ae..2d74bce5f1 100644 --- a/extensions/flac/src/main/java/com/google/android/exoplayer2/ext/flac/FlacDecoder.java +++ b/extensions/flac/src/main/java/com/google/android/exoplayer2/ext/flac/FlacDecoder.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.ext.flac; +import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.SimpleDecoder; import com.google.android.exoplayer2.decoder.SimpleOutputBuffer; @@ -37,11 +38,17 @@ import java.util.List; * * @param numInputBuffers The number of input buffers. * @param numOutputBuffers The number of output buffers. + * @param maxInputBufferSize The maximum required input buffer size if known, or {@link + * Format#NO_VALUE} otherwise. * @param initializationData Codec-specific initialization data. It should contain only one entry - * which is the flac file header. + * which is the flac file header. * @throws FlacDecoderException Thrown if an exception occurs when initializing the decoder. */ - public FlacDecoder(int numInputBuffers, int numOutputBuffers, List initializationData) + public FlacDecoder( + int numInputBuffers, + int numOutputBuffers, + int maxInputBufferSize, + List initializationData) throws FlacDecoderException { super(new DecoderInputBuffer[numInputBuffers], new SimpleOutputBuffer[numOutputBuffers]); if (initializationData.size() != 1) { @@ -60,7 +67,9 @@ import java.util.List; throw new FlacDecoderException("Metadata decoding failed"); } - setInitialInputBufferSize(streamInfo.maxFrameSize); + int initialInputBufferSize = + maxInputBufferSize != Format.NO_VALUE ? maxInputBufferSize : streamInfo.maxFrameSize; + setInitialInputBufferSize(initialInputBufferSize); maxOutputBufferSize = streamInfo.maxDecodedFrameSize(); } diff --git a/extensions/flac/src/main/java/com/google/android/exoplayer2/ext/flac/LibflacAudioRenderer.java b/extensions/flac/src/main/java/com/google/android/exoplayer2/ext/flac/LibflacAudioRenderer.java index a72b03cd44..fa66abbdc6 100644 --- a/extensions/flac/src/main/java/com/google/android/exoplayer2/ext/flac/LibflacAudioRenderer.java +++ b/extensions/flac/src/main/java/com/google/android/exoplayer2/ext/flac/LibflacAudioRenderer.java @@ -65,7 +65,8 @@ public class LibflacAudioRenderer extends SimpleDecoderAudioRenderer { @Override protected FlacDecoder createDecoder(Format format, ExoMediaCrypto mediaCrypto) throws FlacDecoderException { - return new FlacDecoder(NUM_BUFFERS, NUM_BUFFERS, format.initializationData); + return new FlacDecoder( + NUM_BUFFERS, NUM_BUFFERS, format.maxInputSize, format.initializationData); } } diff --git a/extensions/opus/src/main/java/com/google/android/exoplayer2/ext/opus/LibopusAudioRenderer.java b/extensions/opus/src/main/java/com/google/android/exoplayer2/ext/opus/LibopusAudioRenderer.java index b94f3e9332..57937b4282 100644 --- a/extensions/opus/src/main/java/com/google/android/exoplayer2/ext/opus/LibopusAudioRenderer.java +++ b/extensions/opus/src/main/java/com/google/android/exoplayer2/ext/opus/LibopusAudioRenderer.java @@ -30,8 +30,10 @@ import com.google.android.exoplayer2.util.MimeTypes; */ public final class LibopusAudioRenderer extends SimpleDecoderAudioRenderer { + /** The number of input and output buffers. */ private static final int NUM_BUFFERS = 16; - private static final int INITIAL_INPUT_BUFFER_SIZE = 960 * 6; + /** The default input buffer size. */ + private static final int DEFAULT_INPUT_BUFFER_SIZE = 960 * 6; private OpusDecoder decoder; @@ -88,8 +90,15 @@ public final class LibopusAudioRenderer extends SimpleDecoderAudioRenderer { @Override protected OpusDecoder createDecoder(Format format, ExoMediaCrypto mediaCrypto) throws OpusDecoderException { - decoder = new OpusDecoder(NUM_BUFFERS, NUM_BUFFERS, INITIAL_INPUT_BUFFER_SIZE, - format.initializationData, mediaCrypto); + int initialInputBufferSize = + format.maxInputSize != Format.NO_VALUE ? format.maxInputSize : DEFAULT_INPUT_BUFFER_SIZE; + decoder = + new OpusDecoder( + NUM_BUFFERS, + NUM_BUFFERS, + initialInputBufferSize, + format.initializationData, + mediaCrypto); return decoder; } diff --git a/extensions/vp9/src/main/java/com/google/android/exoplayer2/ext/vp9/LibvpxVideoRenderer.java b/extensions/vp9/src/main/java/com/google/android/exoplayer2/ext/vp9/LibvpxVideoRenderer.java index 1dbdfac9ee..08c413aba7 100644 --- a/extensions/vp9/src/main/java/com/google/android/exoplayer2/ext/vp9/LibvpxVideoRenderer.java +++ b/extensions/vp9/src/main/java/com/google/android/exoplayer2/ext/vp9/LibvpxVideoRenderer.java @@ -99,11 +99,8 @@ public class LibvpxVideoRenderer extends BaseRenderer { * requiring multiple output buffers to be dequeued at a time for it to make progress. */ private static final int NUM_OUTPUT_BUFFERS = 8; - /** - * The initial input buffer size. Input buffers are reallocated dynamically if this value is - * insufficient. - */ - private static final int INITIAL_INPUT_BUFFER_SIZE = 768 * 1024; // Value based on cs/SoftVpx.cpp. + /** The default input buffer size. */ + private static final int DEFAULT_INPUT_BUFFER_SIZE = 768 * 1024; // Value based on cs/SoftVpx.cpp. private final boolean scaleToFit; private final boolean disableLoopFilter; @@ -703,11 +700,13 @@ public class LibvpxVideoRenderer extends BaseRenderer { try { long decoderInitializingTimestamp = SystemClock.elapsedRealtime(); TraceUtil.beginSection("createVpxDecoder"); + int initialInputBufferSize = + format.maxInputSize != Format.NO_VALUE ? format.maxInputSize : DEFAULT_INPUT_BUFFER_SIZE; decoder = new VpxDecoder( NUM_INPUT_BUFFERS, NUM_OUTPUT_BUFFERS, - INITIAL_INPUT_BUFFER_SIZE, + initialInputBufferSize, mediaCrypto, disableLoopFilter, useSurfaceYuvOutput);