1 /*
2  * Copyright (C) 2019 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 //#define LOG_NDEBUG 0
17 #define LOG_TAG "NativeMediaCommon"
18 #include <log/log.h>
19 
20 #include <cstdio>
21 #include <cstring>
22 #include <utility>
23 
24 #include "NativeMediaCommon.h"
25 /* TODO(b/153592281)
26  * Note: constants used by the native media tests but not available in media ndk api
27  */
28 const char* AMEDIA_MIMETYPE_VIDEO_VP8 = "video/x-vnd.on2.vp8";
29 const char* AMEDIA_MIMETYPE_VIDEO_VP9 = "video/x-vnd.on2.vp9";
30 const char* AMEDIA_MIMETYPE_VIDEO_AV1 = "video/av01";
31 const char* AMEDIA_MIMETYPE_VIDEO_AVC = "video/avc";
32 const char* AMEDIA_MIMETYPE_VIDEO_HEVC = "video/hevc";
33 const char* AMEDIA_MIMETYPE_VIDEO_MPEG4 = "video/mp4v-es";
34 const char* AMEDIA_MIMETYPE_VIDEO_H263 = "video/3gpp";
35 
36 const char* AMEDIA_MIMETYPE_AUDIO_AMR_NB = "audio/3gpp";
37 const char* AMEDIA_MIMETYPE_AUDIO_AMR_WB = "audio/amr-wb";
38 const char* AMEDIA_MIMETYPE_AUDIO_AAC = "audio/mp4a-latm";
39 const char* AMEDIA_MIMETYPE_AUDIO_FLAC = "audio/flac";
40 const char* AMEDIA_MIMETYPE_AUDIO_VORBIS = "audio/vorbis";
41 const char* AMEDIA_MIMETYPE_AUDIO_OPUS = "audio/opus";
42 
43 /* TODO(b/153592281) */
44 const char* TBD_AMEDIACODEC_PARAMETER_KEY_REQUEST_SYNC_FRAME = "request-sync";
45 const char* TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE = "video-bitrate";
46 const char* TBD_AMEDIACODEC_PARAMETER_KEY_MAX_B_FRAMES = "max-bframes";
47 const char* TBD_AMEDIAFORMAT_KEY_BIT_RATE_MODE = "bitrate-mode";
48 
49 // NDK counterpart of RMS_ERROR_TOLERANCE of CodecDecoderTest class
50 const float kRmsErrorTolerance = 1.05f;
51 
52 // NDK counterpart of Q_DEQ_TIMEOUT_US and RETRY_LIMIT of CodecTestBase class
53 const long kQDeQTimeOutUs = 5000; // block at most 5ms while looking for io buffers
54 const int kRetryLimit = 100; // max poll counter before test aborts and returns error
55 
isCSDIdentical(AMediaFormat * refFormat,AMediaFormat * testFormat)56 bool isCSDIdentical(AMediaFormat* refFormat, AMediaFormat* testFormat) {
57     for (int i = 0;; i++) {
58         std::pair<void*, size_t> refCsd;
59         std::pair<void*, size_t> testCsd;
60         char name[16];
61         snprintf(name, sizeof(name), "csd-%d", i);
62         bool hasRefCSD = AMediaFormat_getBuffer(refFormat, name, &refCsd.first, &refCsd.second);
63         bool hasTestCSD = AMediaFormat_getBuffer(testFormat, name, &testCsd.first, &testCsd.second);
64         if (hasRefCSD != hasTestCSD) {
65             ALOGW("mismatch, ref fmt has CSD %d, test fmt has CSD %d", hasRefCSD, hasTestCSD);
66             return false;
67         }
68         if (hasRefCSD) {
69             if (refCsd.second != testCsd.second) {
70                 ALOGW("ref/test %s buffer sizes are not identical %zu/%zu", name, refCsd.second,
71                       testCsd.second);
72                 return false;
73             }
74             if (memcmp(refCsd.first, testCsd.first, refCsd.second)) {
75                 ALOGW("ref/test %s buffers are not identical", name);
76                 return false;
77             }
78         } else break;
79     }
80     return true;
81 }
82 
83 template <class T>
flattenField(uint8_t * buffer,int * pos,T value)84 void flattenField(uint8_t* buffer, int* pos, T value) {
85     uint8_t* ptr = (buffer + *pos);
86     for (int i = sizeof(T) - 1; i >= 0; i--) {
87         *ptr++ = (uint8_t)((value >> (i * 8)) & 0xff);
88     }
89     *pos += sizeof(T);
90 }
91 
92 template void flattenField<int32_t>(uint8_t* buffer, int* pos, int32_t value);
93 template void flattenField<int64_t>(uint8_t* buffer, int* pos, int64_t value);
94 
isFormatSimilar(AMediaFormat * refFormat,AMediaFormat * testFormat)95 bool isFormatSimilar(AMediaFormat* refFormat, AMediaFormat* testFormat) {
96     const char *refMime = nullptr, *testMime = nullptr;
97     int64_t refKeyDuration, testKeyDuration;
98     bool hasRefMime = AMediaFormat_getString(refFormat, AMEDIAFORMAT_KEY_MIME, &refMime);
99     if (!hasRefMime) return false;
100     bool hasTestMime = AMediaFormat_getString(testFormat, AMEDIAFORMAT_KEY_MIME, &testMime);
101     if (!hasTestMime) return false;
102     if (strcmp(refMime, testMime) != 0) return false;
103     bool hasRefKeyDuration =
104             AMediaFormat_getInt64(refFormat, AMEDIAFORMAT_KEY_DURATION, &refKeyDuration);
105     if (!hasRefKeyDuration) return false;
106     bool hasTestKeyDuration =
107             AMediaFormat_getInt64(testFormat, AMEDIAFORMAT_KEY_DURATION, &testKeyDuration);
108     if (!hasTestKeyDuration) return false;
109     if (refKeyDuration != testKeyDuration) {
110         ALOGW("Duration mismatches ref / test = %lld / %lld", (long long) refKeyDuration,
111               (long long) testKeyDuration);
112         // TODO (b/163477410)(b/163478168)
113 //        return false;
114     }
115     if (!isCSDIdentical(refFormat, testFormat)) return false;
116     if (!strncmp(refMime, "audio/", strlen("audio/"))) {
117         int32_t refSampleRate, testSampleRate, refNumChannels, testNumChannels;
118         bool hasRefSampleRate =
119                 AMediaFormat_getInt32(refFormat, AMEDIAFORMAT_KEY_SAMPLE_RATE, &refSampleRate);
120         if (!hasRefSampleRate) return false;
121         bool hasTestSampleRate =
122                 AMediaFormat_getInt32(testFormat, AMEDIAFORMAT_KEY_SAMPLE_RATE, &testSampleRate);
123         if (!hasTestSampleRate) return false;
124         if (refSampleRate != testSampleRate)return false;
125         bool hasRefNumChannels =
126                 AMediaFormat_getInt32(refFormat, AMEDIAFORMAT_KEY_CHANNEL_COUNT, &refNumChannels);
127         if (!hasRefNumChannels) return false;
128         bool hasTestNumChannels =
129                 AMediaFormat_getInt32(testFormat, AMEDIAFORMAT_KEY_CHANNEL_COUNT, &testNumChannels);
130         if (!hasTestNumChannels) return false;
131         if (refNumChannels != testNumChannels) return false;
132     } else if (!strncmp(refMime, "video/", strlen("video/"))) {
133         int32_t refWidth, testWidth, refHeight, testHeight;
134         bool hasRefWidth = AMediaFormat_getInt32(refFormat, AMEDIAFORMAT_KEY_WIDTH, &refWidth);
135         if (!hasRefWidth) return false;
136         bool hasTestWidth = AMediaFormat_getInt32(testFormat, AMEDIAFORMAT_KEY_WIDTH, &testWidth);
137         if (!hasTestWidth) return false;
138         if (refWidth != testWidth) return false;
139         bool hasRefHeight = AMediaFormat_getInt32(refFormat, AMEDIAFORMAT_KEY_HEIGHT, &refHeight);
140         if (!hasRefHeight) return false;
141         bool hasTestHeight =
142                 AMediaFormat_getInt32(testFormat, AMEDIAFORMAT_KEY_HEIGHT, &testHeight);
143         if (!hasTestHeight) return false;
144         if (refHeight != testHeight) return false;
145     }
146     return true;
147 }
148