Add video speed change effect

This adds an effect to change the speed of the video track. The speed
of the audio track can already be changed using a SonicAudioProcessor.

Issue: androidx/media#559
PiperOrigin-RevId: 555155418
This commit is contained in:
kimvde 2023-08-09 14:23:59 +00:00 committed by Tianyi Feng
parent cf9f048a3d
commit 266dc5a6d6
4 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,69 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.effect;
import static androidx.media3.test.utils.BitmapPixelTestUtil.readBitmap;
import static com.google.common.truth.Truth.assertThat;
import androidx.media3.common.C;
import androidx.media3.common.Effect;
import androidx.media3.common.VideoFrameProcessor;
import androidx.media3.test.utils.VideoFrameProcessorTestRunner;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
/** Tests for {@link SpeedChangeEffect}. */
@RunWith(AndroidJUnit4.class)
public class SpeedChangeEffectTest {
@Rule public final TestName testName = new TestName();
private static final String IMAGE_PATH =
"media/bitmap/sample_mp4_first_frame/electrical_colors/original.png";
@Test
public void changeSpeed_outputsFramesAtTheCorrectPresentationTimesUs() throws Exception {
String testId = testName.getMethodName();
VideoFrameProcessor.Factory videoFrameProcessorFactory =
new DefaultVideoFrameProcessor.Factory.Builder().build();
ImmutableList<Effect> effects = ImmutableList.of(new SpeedChangeEffect(2f));
List<Long> outputPresentationTimesUs = new ArrayList<>();
VideoFrameProcessorTestRunner.OnOutputFrameAvailableForRenderingListener
onOutputFrameAvailableForRenderingListener = outputPresentationTimesUs::add;
VideoFrameProcessorTestRunner videoFrameProcessorTestRunner =
new VideoFrameProcessorTestRunner.Builder()
.setTestId(testId)
.setVideoFrameProcessorFactory(videoFrameProcessorFactory)
.setEffects(effects)
.setOnOutputFrameAvailableForRenderingListener(
onOutputFrameAvailableForRenderingListener)
.build();
videoFrameProcessorTestRunner.queueInputBitmap(
readBitmap(IMAGE_PATH), C.MICROS_PER_SECOND, /* offsetToAddUs= */ 0L, /* frameRate= */ 5);
videoFrameProcessorTestRunner.endFrameProcessing();
assertThat(outputPresentationTimesUs)
.containsExactly(0L, 100_000L, 200_000L, 300_000L, 400_000L)
.inOrder();
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.effect;
import static androidx.media3.common.util.Assertions.checkArgument;
import android.content.Context;
import androidx.annotation.FloatRange;
import androidx.media3.common.VideoFrameProcessingException;
import androidx.media3.common.util.UnstableApi;
/**
* Applies a speed change by updating the frame timestamps.
*
* <p>This effect doesn't drop any frames.
*/
@UnstableApi
public final class SpeedChangeEffect implements GlEffect {
private final float speed;
/** Creates an instance. */
public SpeedChangeEffect(@FloatRange(from = 0, fromInclusive = false) float speed) {
checkArgument(speed > 0f);
this.speed = speed;
}
@Override
public GlShaderProgram toGlShaderProgram(Context context, boolean useHdr)
throws VideoFrameProcessingException {
return new SpeedChangeShaderProgram(context, speed, useHdr);
}
@Override
public boolean isNoOp(int inputWidth, int inputHeight) {
return speed == 1f;
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.effect;
import android.content.Context;
import androidx.media3.common.GlObjectsProvider;
import androidx.media3.common.GlTextureInfo;
import androidx.media3.common.VideoFrameProcessingException;
import androidx.media3.common.util.UnstableApi;
/** Applies a speed change by updating the frame timestamps. */
@UnstableApi
/* package */ final class SpeedChangeShaderProgram extends FrameCacheGlShaderProgram {
private final float speed;
public SpeedChangeShaderProgram(Context context, float speed, boolean useHdr)
throws VideoFrameProcessingException {
super(context, /* capacity= */ 1, useHdr);
this.speed = speed;
}
@Override
public void queueInputFrame(
GlObjectsProvider glObjectsProvider, GlTextureInfo inputTexture, long presentationTimeUs) {
super.queueInputFrame(glObjectsProvider, inputTexture, (long) (presentationTimeUs / speed));
}
}

View File

@ -1604,6 +1604,7 @@ public interface ExoPlayer extends Player {
* <ul> * <ul>
* <li>This feature works only with the default {@link MediaCodecVideoRenderer} and not custom * <li>This feature works only with the default {@link MediaCodecVideoRenderer} and not custom
* or extension {@linkplain Renderer video renderers}. * or extension {@linkplain Renderer video renderers}.
* <li>This feature does not work with {@linkplain Effect effects} updating the timestamps.
* <li>This feature does not work with DRM-protected contents. * <li>This feature does not work with DRM-protected contents.
* <li>This method should be called before calling {@link #prepare}. * <li>This method should be called before calling {@link #prepare}.
* </ul> * </ul>