summaryrefslogtreecommitdiff
path: root/tests/tests/mediaediting/src/android/media/mediaediting/cts/TranscodeQualityTest.java
blob: f72b1b6300c28ea79e0e28b1ab9c07e9e4700e98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * 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
 *
 *      http://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 android.media.mediaediting.cts;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.net.Uri;
import android.platform.test.annotations.AppModeFull;

import androidx.media3.common.Format;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MimeTypes;
import androidx.media3.transformer.EditedMediaItem;
import androidx.media3.transformer.TransformationRequest;
import androidx.media3.transformer.Transformer;
import androidx.test.core.app.ApplicationProvider;

import com.android.compatibility.common.util.ApiTest;
import com.android.compatibility.common.util.Preconditions;

import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/** Instrumentation tests for checking Transcoding quality for given inputs. */
@AppModeFull(reason = "Instant apps cannot access the SD card")
@RunWith(Parameterized.class)
public final class TranscodeQualityTest {
  private static final String MEDIA_DIR = WorkDir.getMediaDirString();
  private static final double EXPECTED_MINIMUM_SSIM = 0.9;

  private final String fromMediaType;
  private final String toMediaType;
  private final int width;
  private final int height;
  private final float frameRate;
  private final String testFile;
  private final boolean isWithinCddRequirements;
  private final String testId;

  public TranscodeQualityTest(String fromMediaType, String toMediaType, int width, int height,
      float frameRate, String testFile, boolean isWithinCddRequirements, String testId) {
    this.fromMediaType = fromMediaType;
    this.toMediaType = toMediaType;
    this.width = width;
    this.height = height;
    this.frameRate = frameRate;
    this.testFile = testFile;
    this.isWithinCddRequirements = isWithinCddRequirements;
    this.testId = testId;
  }

  @Parameterized.Parameters(name = "{index}_{7}")
  public static Collection<Object[]> input() {
    // fromMediaType, toMediaType, width, height, frameRate, clip, isWithinCddRequirements
    // Note: isWithinCddRequirements is the test which we never skip as the input and output formats
    // should be within CDD requirements on all supported API versions.
    final List<Object[]> exhaustiveArgsList = new ArrayList<>(Arrays.asList(new Object[][] {
        {MimeTypes.VIDEO_H264, MimeTypes.VIDEO_H264, 1920, 1080, 30.00f,
            MediaEditingUtil.MP4_ASSET_H264_WITH_INCREASING_TIMESTAMPS_1920W_1080H_1S_URI_STRING,
            false},
        {MimeTypes.VIDEO_H264, MimeTypes.VIDEO_H265, 1920, 1080, 30.00f,
            MediaEditingUtil.MP4_ASSET_H264_WITH_INCREASING_TIMESTAMPS_1920W_1080H_1S_URI_STRING,
            false},
        {MimeTypes.VIDEO_H264, MimeTypes.VIDEO_H264, 320, 240, 30.00f,
            MediaEditingUtil.MP4_ASSET_H264_WITH_INCREASING_TIMESTAMPS_320W_240H_5S_URI_STRING,
            true},
        {MimeTypes.VIDEO_H264, MimeTypes.VIDEO_H265, 320, 240, 30.00f,
            MediaEditingUtil.MP4_ASSET_H264_WITH_INCREASING_TIMESTAMPS_320W_240H_5S_URI_STRING,
            false},
        {MimeTypes.VIDEO_H264, MimeTypes.VIDEO_H265, 642, 642, 30.00f,
            MediaEditingUtil.MP4_ASSET_H264_WITH_INCREASING_TIMESTAMPS_642W_642H_3S_URI_STRING,
            false},
        {MimeTypes.VIDEO_H265, MimeTypes.VIDEO_H265, 1920, 1080, 30.00f,
            MediaEditingUtil.MP4_ASSET_HEVC_WITH_INCREASING_TIMESTAMPS_1920_1080_1S_URI_STRING,
            false},
        {MimeTypes.VIDEO_H265, MimeTypes.VIDEO_H264, 1920, 1080, 30.00f,
            MediaEditingUtil.MP4_ASSET_HEVC_WITH_INCREASING_TIMESTAMPS_1920_1080_1S_URI_STRING,
            false},
        {MimeTypes.VIDEO_H265, MimeTypes.VIDEO_H265, 720, 480, 30.00f,
            MediaEditingUtil.MP4_ASSET_HEVC_WITH_INCREASING_TIMESTAMPS_720W_480H_1S_URI_STRING,
            false},
        {MimeTypes.VIDEO_H265, MimeTypes.VIDEO_H264, 720, 480, 30.00f,
            MediaEditingUtil.MP4_ASSET_HEVC_WITH_INCREASING_TIMESTAMPS_720W_480H_1S_URI_STRING,
            false},
        {MimeTypes.VIDEO_H265, MimeTypes.VIDEO_H264, 642, 642, 30.00f,
            MediaEditingUtil.MP4_ASSET_HEVC_WITH_INCREASING_TIMESTAMPS_642W_642H_3S_URI_STRING,
            false},
    }));
    return prepareParamList(exhaustiveArgsList);
  }

  public static List<Object[]> prepareParamList(List<Object[]> exhaustiveArgsList) {
    List<Object[]> argsList = new ArrayList<>();
    int argLength = exhaustiveArgsList.get(0).length;

    for (Object[] arg : exhaustiveArgsList) {
      String from = arg[0].toString();
      String to = arg[1].toString();
      // Trim the mime baseType with slash.
      int lastIndex = from.lastIndexOf('/');
      if (lastIndex != -1) {
        from = from.substring(lastIndex + 1);
      }
      lastIndex = to.lastIndexOf('/');
      if (lastIndex != -1) {
        to = to.substring(lastIndex + 1);
      }

      String testID = String.format("transcode_%s_To_%s_%dx%d_%dfps_%s_ssim", from, to,
          (int) arg[2], (int) arg[3], Math.round((float) arg[4]),
          (boolean) arg[6] ? "ForceEncoding" : "");
      Object[] argUpdate = new Object[argLength + 1];
      System.arraycopy(arg, 0, argUpdate, 0, argLength);
      argUpdate[argLength] = testID;
      argsList.add(argUpdate);
    }
    return argsList;
  }

  public static Transformer createTransformerForForceEncode(Context context, String toMediaType) {
    return new Transformer.Builder(context)
        .setTransformationRequest(
            new TransformationRequest.Builder().setVideoMimeType(toMediaType).build())
        .setEncoderFactory(new AndroidTestUtil.ForceEncodeEncoderFactory(context))
        .build();
  }

  public static Transformer createTransformer(Context context, String toMediaType) {
    return (new Transformer.Builder(context)
        .setTransformationRequest(
            new TransformationRequest.Builder().setVideoMimeType(toMediaType).build())
        .build());
  }

  @ApiTest(apis = {"android.media.MediaCodec#configure",
      "android.media.MediaCodec#createByCodecName",
      "android.media.MediaCodecList#findDecoderForFormat",
      "android.media.MediaFormat#createVideoFormat"})
  @Test
  public void transcodeTest() throws Exception {
    Format decFormat = new Format.Builder()
        .setSampleMimeType(fromMediaType)
        .setWidth(width)
        .setHeight(height)
        .setFrameRate(frameRate)
        .build();
    Format encFormat = new Format.Builder()
        .setSampleMimeType(toMediaType)
        .setWidth(width)
        .setHeight(height)
        .setFrameRate(frameRate)
        .build();

    Preconditions.assertTestFileExists(MEDIA_DIR + testFile);
    Context context = ApplicationProvider.getApplicationContext();
    if (!isWithinCddRequirements) {
      Assume.assumeTrue("Skipping transcodeTest for " + testId,
          !AndroidTestUtil.skipAndLogIfFormatsUnsupported(
              context, testId, decFormat, encFormat));
    }

    Transformer transformer;
    if (isWithinCddRequirements) {
      transformer = createTransformerForForceEncode(context, toMediaType);
    } else {
      transformer = createTransformer(context, toMediaType);
    }

    MediaItem mediaItem = MediaItem.fromUri(Uri.parse(MEDIA_DIR + testFile));
    EditedMediaItem editedMediaItem = new EditedMediaItem.Builder(mediaItem).setRemoveAudio(true)
        .build();
    ExportTestResult result =
        new TransformerAndroidTestRunner.Builder(context, transformer)
            .setRequestCalculateSsim(true)
            .build()
            .run(testId, editedMediaItem);

    if (!isWithinCddRequirements) {
        Assume.assumeFalse("Skipping transcodeTest for " + testId,
            result.fallbackDetails != null
            && result.fallbackDetails.fallbackOutputHeight != height);
    }
    assertThat(result.ssim).isGreaterThan(EXPECTED_MINIMUM_SSIM);
  }
}