Use audio passthrough if flattening is requested but not needed.

When the input is not a slow motion video, then flattening should do
nothing, so there is no need to re-encode audio.

PiperOrigin-RevId: 412443097
This commit is contained in:
hschlueter 2021-11-26 14:47:27 +00:00 committed by tonihei
parent 51762a4795
commit 041c3e9971

View file

@ -19,12 +19,15 @@ package com.google.android.exoplayer2.transformer;
import static com.google.android.exoplayer2.source.SampleStream.FLAG_REQUIRE_FORMAT; import static com.google.android.exoplayer2.source.SampleStream.FLAG_REQUIRE_FORMAT;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder; import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.mp4.SlowMotionData;
import com.google.android.exoplayer2.source.SampleStream.ReadDataResult; import com.google.android.exoplayer2.source.SampleStream.ReadDataResult;
@RequiresApi(18) @RequiresApi(18)
@ -59,13 +62,29 @@ import com.google.android.exoplayer2.source.SampleStream.ReadDataResult;
return false; return false;
} }
Format inputFormat = checkNotNull(formatHolder.format); Format inputFormat = checkNotNull(formatHolder.format);
if ((transformation.audioMimeType != null boolean shouldChangeMimeType =
&& !transformation.audioMimeType.equals(inputFormat.sampleMimeType)) transformation.audioMimeType != null
|| transformation.flattenForSlowMotion) { && !transformation.audioMimeType.equals(inputFormat.sampleMimeType);
boolean shouldFlattenForSlowMotion =
transformation.flattenForSlowMotion && isSlowMotion(inputFormat);
if (shouldChangeMimeType || shouldFlattenForSlowMotion) {
samplePipeline = new AudioSamplePipeline(inputFormat, transformation, getIndex()); samplePipeline = new AudioSamplePipeline(inputFormat, transformation, getIndex());
} else { } else {
samplePipeline = new PassthroughSamplePipeline(inputFormat); samplePipeline = new PassthroughSamplePipeline(inputFormat);
} }
return true; return true;
} }
private static boolean isSlowMotion(Format format) {
@Nullable Metadata metadata = format.metadata;
if (metadata == null) {
return false;
}
for (int i = 0; i < metadata.length(); i++) {
if (metadata.get(i) instanceof SlowMotionData) {
return true;
}
}
return false;
}
} }