blob: 42933e67dee352bd18f53834cda9b457f7ae0491 [file] [log] [blame]
Linus Nilsson8f0b8762020-07-23 17:12:45 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Native media transcoder library benchmark tests.
19 *
20 * How to run the benchmark:
21 *
22 * 1. Download the media assets from http://go/transcodingbenchmark and push the directory
23 * ("TranscodingBenchmark") to /data/local/tmp.
24 *
25 * 2. Compile the benchmark and sync to device:
26 * $ mm -j72 && adb sync
27 *
28 * 3. Run:
29 * $ adb shell /data/nativetest64/MediaTranscoderBenchmark/MediaTranscoderBenchmark
30 */
31
32#include <benchmark/benchmark.h>
Naveen Kumar Ponnusamy2d074ba2020-12-17 17:13:45 -080033#include <binder/ProcessState.h>
Linus Nilsson8f0b8762020-07-23 17:12:45 -070034#include <fcntl.h>
35#include <media/MediaTranscoder.h>
Linus Nilsson4aef0de2021-01-29 17:58:55 -080036#include <media/NdkCommon.h>
Linus Nilsson47352412020-12-16 12:21:26 -080037
Naveen Kumar Ponnusamy8b8e7002020-12-02 12:01:32 +053038#include <iostream>
Linus Nilsson8f0b8762020-07-23 17:12:45 -070039
40using namespace android;
41
Naveen Kumar Ponnusamy8b8e7002020-12-02 12:01:32 +053042const std::string PARAM_VIDEO_FRAME_RATE = "VideoFrameRate";
43
Linus Nilsson8f0b8762020-07-23 17:12:45 -070044class TranscoderCallbacks : public MediaTranscoder::CallbackInterface {
45public:
46 virtual void onFinished(const MediaTranscoder* transcoder __unused) override {
47 std::unique_lock<std::mutex> lock(mMutex);
48 mFinished = true;
49 mCondition.notify_all();
50 }
51
52 virtual void onError(const MediaTranscoder* transcoder __unused,
53 media_status_t error) override {
54 std::unique_lock<std::mutex> lock(mMutex);
55 mFinished = true;
56 mStatus = error;
57 mCondition.notify_all();
58 }
59
60 virtual void onProgressUpdate(const MediaTranscoder* transcoder __unused,
61 int32_t progress __unused) override {}
62
63 virtual void onCodecResourceLost(const MediaTranscoder* transcoder __unused,
Chong Zhange4e088f2020-10-21 19:10:42 -070064 const std::shared_ptr<ndk::ScopedAParcel>& pausedState
Linus Nilsson8f0b8762020-07-23 17:12:45 -070065 __unused) override {}
66
67 bool waitForTranscodingFinished() {
68 std::unique_lock<std::mutex> lock(mMutex);
69 while (!mFinished) {
70 if (mCondition.wait_for(lock, std::chrono::minutes(5)) == std::cv_status::timeout) {
71 return false;
72 }
73 }
74 return true;
75 }
76
77 media_status_t mStatus = AMEDIA_OK;
78
79private:
80 std::mutex mMutex;
81 std::condition_variable mCondition;
82 bool mFinished = false;
83};
84
Linus Nilsson16d772b2020-09-29 19:21:11 -070085static AMediaFormat* CreateDefaultVideoFormat() {
Linus Nilsson8f0b8762020-07-23 17:12:45 -070086 // Default bitrate
87 static constexpr int32_t kVideoBitRate = 20 * 1000 * 1000; // 20Mbs
Linus Nilsson16d772b2020-09-29 19:21:11 -070088
89 AMediaFormat* videoFormat = AMediaFormat_new();
90 AMediaFormat_setInt32(videoFormat, AMEDIAFORMAT_KEY_BIT_RATE, kVideoBitRate);
Linus Nilsson4aef0de2021-01-29 17:58:55 -080091 AMediaFormat_setString(videoFormat, AMEDIAFORMAT_KEY_MIME, AMEDIA_MIMETYPE_VIDEO_AVC);
Linus Nilsson16d772b2020-09-29 19:21:11 -070092 return videoFormat;
93}
94
95/**
96 * Callback to configure tracks for transcoding.
97 * @param mime The source track mime type.
98 * @param dstFormat The destination format if the track should be transcoded or nullptr if the track
99 * should be passed through.
100 * @return True if the track should be included in the output file.
101 */
102using TrackSelectionCallback = std::function<bool(const char* mime, AMediaFormat** dstFormat)>;
103
104static void TranscodeMediaFile(benchmark::State& state, const std::string& srcFileName,
105 const std::string& dstFileName,
106 TrackSelectionCallback trackSelectionCallback) {
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700107 // Write-only, create file if non-existent.
108 static constexpr int kDstOpenFlags = O_WRONLY | O_CREAT;
109 // User R+W permission.
110 static constexpr int kDstFileMode = S_IRUSR | S_IWUSR;
111 // Asset directory
112 static const std::string kAssetDirectory = "/data/local/tmp/TranscodingBenchmark/";
113
114 int srcFd = 0;
115 int dstFd = 0;
116
117 std::string srcPath = kAssetDirectory + srcFileName;
118 std::string dstPath = kAssetDirectory + dstFileName;
119
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700120 media_status_t status = AMEDIA_OK;
121
122 if ((srcFd = open(srcPath.c_str(), O_RDONLY)) < 0) {
123 state.SkipWithError("Unable to open source file");
124 goto exit;
125 }
126 if ((dstFd = open(dstPath.c_str(), kDstOpenFlags, kDstFileMode)) < 0) {
127 state.SkipWithError("Unable to open destination file");
128 goto exit;
129 }
130
131 for (auto _ : state) {
Naveen Kumar Ponnusamy78d362a2020-12-16 18:10:47 -0800132 auto callbacks = std::make_shared<TranscoderCallbacks>();
Chong Zhangbbb4eac2020-11-18 11:12:06 -0800133 auto transcoder = MediaTranscoder::create(callbacks);
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700134
135 status = transcoder->configureSource(srcFd);
136 if (status != AMEDIA_OK) {
137 state.SkipWithError("Unable to configure transcoder source");
138 goto exit;
139 }
140
141 status = transcoder->configureDestination(dstFd);
142 if (status != AMEDIA_OK) {
143 state.SkipWithError("Unable to configure transcoder destination");
144 goto exit;
145 }
146
147 std::vector<std::shared_ptr<AMediaFormat>> trackFormats = transcoder->getTrackFormats();
148 for (int i = 0; i < trackFormats.size(); ++i) {
149 AMediaFormat* srcFormat = trackFormats[i].get();
150 AMediaFormat* dstFormat = nullptr;
151
152 const char* mime = nullptr;
153 if (!AMediaFormat_getString(srcFormat, AMEDIAFORMAT_KEY_MIME, &mime)) {
154 state.SkipWithError("Source track format does not have MIME type");
155 goto exit;
156 }
157
158 if (strncmp(mime, "video/", 6) == 0) {
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700159 int32_t frameCount;
160 if (AMediaFormat_getInt32(srcFormat, AMEDIAFORMAT_KEY_FRAME_COUNT, &frameCount)) {
Naveen Kumar Ponnusamy0dbe8712020-12-16 23:31:38 -0800161 state.counters[PARAM_VIDEO_FRAME_RATE] = benchmark::Counter(
162 frameCount, benchmark::Counter::kIsIterationInvariantRate);
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700163 }
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700164 }
165
Linus Nilsson16d772b2020-09-29 19:21:11 -0700166 if (trackSelectionCallback(mime, &dstFormat)) {
167 status = transcoder->configureTrackFormat(i, dstFormat);
168 }
169
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700170 if (dstFormat != nullptr) {
171 AMediaFormat_delete(dstFormat);
172 }
173 if (status != AMEDIA_OK) {
174 state.SkipWithError("Unable to configure track");
175 goto exit;
176 }
177 }
178
179 status = transcoder->start();
180 if (status != AMEDIA_OK) {
181 state.SkipWithError("Unable to start transcoder");
182 goto exit;
183 }
184
185 if (!callbacks->waitForTranscodingFinished()) {
Linus Nilssonb09aac22020-07-29 11:56:53 -0700186 transcoder->cancel();
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700187 state.SkipWithError("Transcoder timed out");
188 goto exit;
189 }
190 if (callbacks->mStatus != AMEDIA_OK) {
191 state.SkipWithError("Transcoder error when running");
192 goto exit;
193 }
194 }
195
196exit:
197 if (srcFd > 0) close(srcFd);
198 if (dstFd > 0) close(dstFd);
199}
200
Linus Nilsson6bf46532020-10-07 11:58:02 -0700201/**
202 * Callback to edit track format for transcoding.
203 * @param dstFormat The default track format for the track type.
204 */
205using TrackFormatEditCallback = std::function<void(AMediaFormat* dstFormat)>;
206
Linus Nilsson16d772b2020-09-29 19:21:11 -0700207static void TranscodeMediaFile(benchmark::State& state, const std::string& srcFileName,
208 const std::string& dstFileName, bool includeAudio,
Linus Nilsson6bf46532020-10-07 11:58:02 -0700209 bool transcodeVideo,
210 const TrackFormatEditCallback& videoFormatEditor = nullptr) {
Linus Nilsson16d772b2020-09-29 19:21:11 -0700211 TranscodeMediaFile(state, srcFileName, dstFileName,
212 [=](const char* mime, AMediaFormat** dstFormatOut) -> bool {
213 *dstFormatOut = nullptr;
214 if (strncmp(mime, "video/", 6) == 0 && transcodeVideo) {
215 *dstFormatOut = CreateDefaultVideoFormat();
Linus Nilsson6bf46532020-10-07 11:58:02 -0700216 if (videoFormatEditor != nullptr) {
217 videoFormatEditor(*dstFormatOut);
218 }
Linus Nilsson16d772b2020-09-29 19:21:11 -0700219 } else if (strncmp(mime, "audio/", 6) == 0 && !includeAudio) {
220 return false;
221 }
222 return true;
223 });
224}
225
Linus Nilsson6bf46532020-10-07 11:58:02 -0700226static void SetMaxOperatingRate(AMediaFormat* format) {
Linus Nilsson4aef0de2021-01-29 17:58:55 -0800227 AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_OPERATING_RATE, INT32_MAX);
Linus Nilsson6bf46532020-10-07 11:58:02 -0700228 AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_PRIORITY, 1);
229}
230
231//-------------------------------- AVC to AVC Benchmarks -------------------------------------------
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700232
233static void BM_TranscodeAvc2AvcAudioVideo2AudioVideo(benchmark::State& state) {
234 TranscodeMediaFile(state, "video_1920x1080_3648frame_h264_22Mbps_30fps_aac.mp4",
235 "video_1920x1080_3648frame_h264_22Mbps_30fps_aac_transcoded_AV.mp4",
Linus Nilsson16d772b2020-09-29 19:21:11 -0700236 true /* includeAudio */, true /* transcodeVideo */);
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700237}
238
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700239static void BM_TranscodeAvc2AvcVideo2Video(benchmark::State& state) {
240 TranscodeMediaFile(state, "video_1920x1080_3648frame_h264_22Mbps_30fps.mp4",
241 "video_1920x1080_3648frame_h264_22Mbps_30fps_transcoded_V.mp4",
Linus Nilsson16d772b2020-09-29 19:21:11 -0700242 false /* includeAudio */, true /* transcodeVideo */);
243}
244
245static void BM_TranscodeAvc2AvcAV2AVMaxOperatingRate(benchmark::State& state) {
246 TranscodeMediaFile(state, "video_1920x1080_3648frame_h264_22Mbps_30fps_aac.mp4",
247 "video_1920x1080_3648frame_h264_22Mbps_30fps_aac_transcoded_AV.mp4",
Linus Nilsson6bf46532020-10-07 11:58:02 -0700248 true /* includeAudio */, true /* transcodeVideo */, SetMaxOperatingRate);
Linus Nilsson16d772b2020-09-29 19:21:11 -0700249}
250
251static void BM_TranscodeAvc2AvcV2VMaxOperatingRate(benchmark::State& state) {
252 TranscodeMediaFile(state, "video_1920x1080_3648frame_h264_22Mbps_30fps.mp4",
253 "video_1920x1080_3648frame_h264_22Mbps_30fps_transcoded_V.mp4",
Linus Nilsson6bf46532020-10-07 11:58:02 -0700254 false /* includeAudio */, true /* transcodeVideo */, SetMaxOperatingRate);
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700255}
256
Linus Nilssona7aa2f62020-10-29 13:19:35 -0700257static void BM_TranscodeAvc2AvcAV2AV720P(benchmark::State& state) {
258 TranscodeMediaFile(state, "video_1280x720_3648frame_h264_16Mbps_30fps_aac.mp4",
259 "video_1280x720_3648frame_h264_16Mbps_30fps_aac_transcoded_AV.mp4",
260 true /* includeAudio */, true /* transcodeVideo */);
261}
262
263static void BM_TranscodeAvc2AvcAV2AV720PMaxOperatingRate(benchmark::State& state) {
264 TranscodeMediaFile(state, "video_1280x720_3648frame_h264_16Mbps_30fps_aac.mp4",
265 "video_1280x720_3648frame_h264_16Mbps_30fps_aac_transcoded_AV.mp4",
266 true /* includeAudio */, true /* transcodeVideo */, SetMaxOperatingRate);
267}
Linus Nilsson6bf46532020-10-07 11:58:02 -0700268//-------------------------------- HEVC to AVC Benchmarks ------------------------------------------
269
270static void BM_TranscodeHevc2AvcAudioVideo2AudioVideo(benchmark::State& state) {
271 TranscodeMediaFile(state, "video_1920x1080_3863frame_hevc_4Mbps_30fps_aac.mp4",
272 "video_1920x1080_3863frame_hevc_4Mbps_30fps_aac_transcoded_AV.mp4",
273 true /* includeAudio */, true /* transcodeVideo */);
274}
275
276static void BM_TranscodeHevc2AvcVideo2Video(benchmark::State& state) {
277 TranscodeMediaFile(state, "video_1920x1080_3863frame_hevc_4Mbps_30fps.mp4",
278 "video_1920x1080_3863frame_hevc_4Mbps_30fps_transcoded_V.mp4",
279 false /* includeAudio */, true /* transcodeVideo */);
280}
281
282static void BM_TranscodeHevc2AvcAV2AVMaxOperatingRate(benchmark::State& state) {
283 TranscodeMediaFile(state, "video_1920x1080_3863frame_hevc_4Mbps_30fps_aac.mp4",
284 "video_1920x1080_3863frame_hevc_4Mbps_30fps_aac_transcoded_AV.mp4",
285 true /* includeAudio */, true /* transcodeVideo */, SetMaxOperatingRate);
286}
287
288static void BM_TranscodeHevc2AvcV2VMaxOperatingRate(benchmark::State& state) {
289 TranscodeMediaFile(state, "video_1920x1080_3863frame_hevc_4Mbps_30fps.mp4",
290 "video_1920x1080_3863frame_hevc_4Mbps_30fps_transcoded_V.mp4",
291 false /* includeAudio */, true /* transcodeVideo */, SetMaxOperatingRate);
292}
293
Linus Nilssona7aa2f62020-10-29 13:19:35 -0700294static void BM_TranscodeHevc2AvcAV2AV720P(benchmark::State& state) {
295 TranscodeMediaFile(state, "video_1280x720_3863frame_hevc_16Mbps_30fps_aac.mp4",
296 "video_1280x720_3863frame_hevc_16Mbps_30fps_aac_transcoded_AV.mp4",
297 true /* includeAudio */, true /* transcodeVideo */);
298}
299
300static void BM_TranscodeHevc2AvcAV2AV720PMaxOperatingRate(benchmark::State& state) {
301 TranscodeMediaFile(state, "video_1280x720_3863frame_hevc_16Mbps_30fps_aac.mp4",
302 "video_1280x720_3863frame_hevc_16Mbps_30fps_aac_transcoded_AV.mp4",
303 true /* includeAudio */, true /* transcodeVideo */, SetMaxOperatingRate);
304}
305
Linus Nilsson6bf46532020-10-07 11:58:02 -0700306//-------------------------------- Passthrough Benchmarks ------------------------------------------
307
Linus Nilsson6233fed2020-08-13 15:15:14 -0700308static void BM_TranscodeAudioVideoPassthrough(benchmark::State& state) {
309 TranscodeMediaFile(state, "video_1920x1080_3648frame_h264_22Mbps_30fps_aac.mp4",
310 "video_1920x1080_3648frame_h264_22Mbps_30fps_aac_passthrough_AV.mp4",
311 true /* includeAudio */, false /* transcodeVideo */);
312}
313static void BM_TranscodeVideoPassthrough(benchmark::State& state) {
314 TranscodeMediaFile(state, "video_1920x1080_3648frame_h264_22Mbps_30fps.mp4",
315 "video_1920x1080_3648frame_h264_22Mbps_30fps_passthrough_AV.mp4",
316 false /* includeAudio */, false /* transcodeVideo */);
317}
318
Linus Nilsson6bf46532020-10-07 11:58:02 -0700319//-------------------------------- Benchmark Registration ------------------------------------------
320
321// Benchmark registration wrapper for transcoding.
322#define TRANSCODER_BENCHMARK(func) \
323 BENCHMARK(func)->UseRealTime()->MeasureProcessCPUTime()->Unit(benchmark::kMillisecond)
324
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700325TRANSCODER_BENCHMARK(BM_TranscodeAvc2AvcAudioVideo2AudioVideo);
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700326TRANSCODER_BENCHMARK(BM_TranscodeAvc2AvcVideo2Video);
Linus Nilsson16d772b2020-09-29 19:21:11 -0700327TRANSCODER_BENCHMARK(BM_TranscodeAvc2AvcAV2AVMaxOperatingRate);
328TRANSCODER_BENCHMARK(BM_TranscodeAvc2AvcV2VMaxOperatingRate);
Linus Nilssona7aa2f62020-10-29 13:19:35 -0700329TRANSCODER_BENCHMARK(BM_TranscodeAvc2AvcAV2AV720P);
330TRANSCODER_BENCHMARK(BM_TranscodeAvc2AvcAV2AV720PMaxOperatingRate);
Linus Nilsson6bf46532020-10-07 11:58:02 -0700331
332TRANSCODER_BENCHMARK(BM_TranscodeHevc2AvcAudioVideo2AudioVideo);
333TRANSCODER_BENCHMARK(BM_TranscodeHevc2AvcVideo2Video);
334TRANSCODER_BENCHMARK(BM_TranscodeHevc2AvcAV2AVMaxOperatingRate);
335TRANSCODER_BENCHMARK(BM_TranscodeHevc2AvcV2VMaxOperatingRate);
Linus Nilssona7aa2f62020-10-29 13:19:35 -0700336TRANSCODER_BENCHMARK(BM_TranscodeHevc2AvcAV2AV720P);
337TRANSCODER_BENCHMARK(BM_TranscodeHevc2AvcAV2AV720PMaxOperatingRate);
Linus Nilsson6bf46532020-10-07 11:58:02 -0700338
Linus Nilsson6233fed2020-08-13 15:15:14 -0700339TRANSCODER_BENCHMARK(BM_TranscodeAudioVideoPassthrough);
340TRANSCODER_BENCHMARK(BM_TranscodeVideoPassthrough);
Linus Nilsson8f0b8762020-07-23 17:12:45 -0700341
Naveen Kumar Ponnusamy8b8e7002020-12-02 12:01:32 +0530342class CustomCsvReporter : public benchmark::BenchmarkReporter {
343public:
344 CustomCsvReporter() : mPrintedHeader(false) {}
345 virtual bool ReportContext(const Context& context);
346 virtual void ReportRuns(const std::vector<Run>& reports);
347
348private:
349 void PrintRunData(const Run& report);
350
351 bool mPrintedHeader;
352 std::vector<std::string> mHeaders = {"name", "real_time", "cpu_time", PARAM_VIDEO_FRAME_RATE};
353};
354
355bool CustomCsvReporter::ReportContext(const Context& context __unused) {
356 return true;
357}
358
359void CustomCsvReporter::ReportRuns(const std::vector<Run>& reports) {
360 std::ostream& Out = GetOutputStream();
361
362 if (!mPrintedHeader) {
363 // print the header
364 for (auto header = mHeaders.begin(); header != mHeaders.end();) {
365 Out << *header++;
366 if (header != mHeaders.end()) Out << ",";
367 }
368 Out << "\n";
369 mPrintedHeader = true;
370 }
371
372 // print results for each run
373 for (const auto& run : reports) {
374 PrintRunData(run);
375 }
376}
377
378void CustomCsvReporter::PrintRunData(const Run& run) {
379 if (run.error_occurred) {
380 return;
381 }
382 std::ostream& Out = GetOutputStream();
383 Out << run.benchmark_name() << ",";
384 Out << run.GetAdjustedRealTime() << ",";
385 Out << run.GetAdjustedCPUTime() << ",";
386 auto frameRate = run.counters.find(PARAM_VIDEO_FRAME_RATE);
387 if (frameRate == run.counters.end()) {
388 Out << "NA"
389 << ",";
390 } else {
391 Out << frameRate->second << ",";
392 }
393 Out << '\n';
394}
395
396int main(int argc, char** argv) {
Naveen Kumar Ponnusamy2d074ba2020-12-17 17:13:45 -0800397 android::ProcessState::self()->startThreadPool();
Naveen Kumar Ponnusamy8b8e7002020-12-02 12:01:32 +0530398 std::unique_ptr<benchmark::BenchmarkReporter> fileReporter;
399 for (int i = 1; i < argc; ++i) {
400 if (std::string(argv[i]).find("--benchmark_out") != std::string::npos) {
401 fileReporter.reset(new CustomCsvReporter);
402 break;
403 }
404 }
405 ::benchmark::Initialize(&argc, argv);
406 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
407 ::benchmark::RunSpecifiedBenchmarks(nullptr, fileReporter.get());
408}