1 /*
2  * Copyright (C) 2021 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 // Unit Test for HDR to SDR transcoding.
18 
19 // #define LOG_NDEBUG 0
20 #define LOG_TAG "HdrTranscodeTests"
21 
22 #include <android-base/logging.h>
23 #include <android-base/properties.h>
24 #include <android/binder_process.h>
25 #include <fcntl.h>
26 #include <gtest/gtest.h>
27 #include <media/MediaSampleReaderNDK.h>
28 #include <media/MediaTranscoder.h>
29 #include <media/NdkCommon.h>
30 
31 #include "TranscoderTestUtils.h"
32 
33 namespace android {
34 
35 // Debug property to load the sample HDR plugin.
36 static const std::string kLoadSamplePluginProperty{"debug.codec2.force-sample-plugin"};
37 
38 // SDR color standard, from MediaFormat.
39 static constexpr int COLOR_STANDARD_BT709 = 1;
40 
41 class HdrTranscodeTests : public ::testing::Test {
42 public:
HdrTranscodeTests()43     HdrTranscodeTests() { LOG(DEBUG) << "HdrTranscodeTests created"; }
~HdrTranscodeTests()44     ~HdrTranscodeTests() { LOG(DEBUG) << "HdrTranscodeTests destroyed"; }
45 
SetUp()46     void SetUp() override {
47         LOG(DEBUG) << "HdrTranscodeTests set up";
48         mCallbacks = std::make_shared<TestTranscoderCallbacks>();
49         ABinderProcess_startThreadPool();
50     }
51 
TearDown()52     void TearDown() override {
53         LOG(DEBUG) << "HdrTranscodeTests tear down";
54         mCallbacks.reset();
55     }
56 
transcode(const char * srcFile,const char * dstFile,const char * dstMime)57     media_status_t transcode(const char* srcFile, const char* dstFile, const char* dstMime) {
58         std::string srcPath = mSrcDir + srcFile;
59         std::string dstPath = mDstDir + dstFile;
60 
61         auto transcoder = MediaTranscoder::create(mCallbacks, -1 /*heartBeatIntervalUs*/);
62         EXPECT_NE(transcoder, nullptr);
63 
64         const int srcFd = open(srcPath.c_str(), O_RDONLY);
65         EXPECT_EQ(transcoder->configureSource(srcFd), AMEDIA_OK);
66         close(srcFd);
67 
68         std::vector<std::shared_ptr<AMediaFormat>> trackFormats = transcoder->getTrackFormats();
69         EXPECT_GT(trackFormats.size(), 0);
70 
71         for (int i = 0; i < trackFormats.size(); ++i) {
72             std::shared_ptr<AMediaFormat> format;
73             const char* mime = nullptr;
74 
75             AMediaFormat_getString(trackFormats[i].get(), AMEDIAFORMAT_KEY_MIME, &mime);
76             if (strncmp(mime, "video/", 6) == 0) {
77                 format = std::shared_ptr<AMediaFormat>(AMediaFormat_new(), &AMediaFormat_delete);
78                 AMediaFormat_setString(format.get(), AMEDIAFORMAT_KEY_MIME, dstMime);
79             }
80 
81             media_status_t status = transcoder->configureTrackFormat(i, format.get());
82             if (status != AMEDIA_OK) {
83                 return status;
84             }
85         }
86 
87         const int dstFd = open(dstPath.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
88         EXPECT_EQ(transcoder->configureDestination(dstFd), AMEDIA_OK);
89         close(dstFd);
90 
91         media_status_t startStatus = transcoder->start();
92         EXPECT_EQ(startStatus, AMEDIA_OK);
93         if (startStatus != AMEDIA_OK) {
94             return startStatus;
95         }
96 
97         mCallbacks->waitForTranscodingFinished();
98         return mCallbacks->mStatus;
99     }
100 
validateOutput(const char * dstFile __unused)101     media_status_t validateOutput(const char* dstFile __unused) {
102         std::string path = mDstDir + dstFile;
103 
104         auto format = TranscoderTestUtils::GetVideoFormat(path);
105         EXPECT_NE(format.get(), nullptr);
106 
107         int32_t value;
108         EXPECT_TRUE(AMediaFormat_getInt32(format.get(), AMEDIAFORMAT_KEY_COLOR_STANDARD, &value));
109         EXPECT_EQ(value, COLOR_STANDARD_BT709);
110 
111         EXPECT_TRUE(AMediaFormat_getInt32(format.get(), AMEDIAFORMAT_KEY_COLOR_TRANSFER, &value));
112         EXPECT_EQ(value, COLOR_TRANSFER_SDR_VIDEO);
113 
114         // TODO(lnilsson): Validate decoded pixels as well. Either by comparing similarity against a
115         //  known good "golden master" corresponding SDR video, or by looking at the histogram.
116         return AMEDIA_OK;
117     }
118 
hdrToSdrConversionSupported(const char * hdrFile)119     bool hdrToSdrConversionSupported(const char* hdrFile) {
120         std::string srcPath = mSrcDir + hdrFile;
121 
122         std::string mime;
123         auto format = TranscoderTestUtils::GetVideoFormat(srcPath, &mime);
124         EXPECT_NE(format.get(), nullptr);
125 
126         AMediaCodec* decoder = AMediaCodec_createDecoderByType(mime.c_str());
127         EXPECT_NE(decoder, nullptr);
128 
129         AMediaFormat_setInt32(format.get(), TBD_AMEDIACODEC_PARAMETER_KEY_COLOR_TRANSFER_REQUEST,
130                               COLOR_TRANSFER_SDR_VIDEO);
131 
132         EXPECT_EQ(AMediaCodec_configure(decoder, format.get(), nullptr /*surface*/,
133                                         nullptr /*crypto*/, 0 /*flags*/),
134                   AMEDIA_OK);
135 
136         AMediaFormat* inputFormat = AMediaCodec_getInputFormat(decoder);
137         EXPECT_NE(inputFormat, nullptr);
138 
139         int32_t transferFunc;
140         bool conversionSupported =
141                 AMediaFormat_getInt32(inputFormat,
142                                       TBD_AMEDIACODEC_PARAMETER_KEY_COLOR_TRANSFER_REQUEST,
143                                       &transferFunc) &&
144                 transferFunc == COLOR_TRANSFER_SDR_VIDEO;
145 
146         AMediaFormat_delete(inputFormat);
147         AMediaCodec_delete(decoder);
148 
149         return conversionSupported;
150     }
151 
152     std::shared_ptr<TestTranscoderCallbacks> mCallbacks;
153     const std::string mSrcDir{"/data/local/tmp/TranscodingTestAssets/"};
154     const std::string mDstDir{"/data/local/tmp/"};
155 };
156 
TEST_F(HdrTranscodeTests,TestHdrSamplePluginTranscode)157 TEST_F(HdrTranscodeTests, TestHdrSamplePluginTranscode) {
158     const char* hdrFile = "video_1280x720_hevc_hdr10_static_3mbps.mp4";
159     const char* dstFile = "video_1280x720_hevc_hdr10_static_3mbps_transcoded.mp4";
160 
161     EXPECT_TRUE(android::base::SetProperty(kLoadSamplePluginProperty, "true"));
162 
163     if (hdrToSdrConversionSupported(hdrFile)) {
164         LOG(INFO) << "HDR -> SDR supported, validating output..";
165         EXPECT_EQ(transcode(hdrFile, dstFile, AMEDIA_MIMETYPE_VIDEO_AVC), AMEDIA_OK);
166         EXPECT_EQ(validateOutput(dstFile), AMEDIA_OK);
167     } else {
168         LOG(INFO) << "HDR -> SDR *not* supported";
169         EXPECT_EQ(transcode(hdrFile, dstFile, AMEDIA_MIMETYPE_VIDEO_AVC), AMEDIA_ERROR_UNSUPPORTED);
170     }
171 
172     EXPECT_TRUE(android::base::SetProperty(kLoadSamplePluginProperty, "false"));
173 }
174 }  // namespace android
175 
main(int argc,char ** argv)176 int main(int argc, char** argv) {
177     ::testing::InitGoogleTest(&argc, argv);
178     return RUN_ALL_TESTS();
179 }
180