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_VORBIS = "audio/vorbis";
40 const char* AMEDIA_MIMETYPE_AUDIO_OPUS = "audio/opus";
41 
42 /* TODO(b/153592281) */
43 const char* TBD_AMEDIACODEC_PARAMETER_KEY_REQUEST_SYNC_FRAME = "request-sync";
44 const char* TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE = "video-bitrate";
45 const char* TBD_AMEDIACODEC_PARAMETER_KEY_MAX_B_FRAMES = "max-bframes";
46 const char* TBD_AMEDIAFORMAT_KEY_BIT_RATE_MODE = "bitrate-mode";
47 
isCSDIdentical(AMediaFormat * refFormat,AMediaFormat * testFormat)48 bool isCSDIdentical(AMediaFormat* refFormat, AMediaFormat* testFormat) {
49     const char* mime;
50     AMediaFormat_getString(refFormat, AMEDIAFORMAT_KEY_MIME, &mime);
51     for (int i = 0;; i++) {
52         std::pair<void*, size_t> refCsd;
53         std::pair<void*, size_t> testCsd;
54         char name[16];
55         snprintf(name, sizeof(name), "csd-%d", i);
56         bool hasRefCSD = AMediaFormat_getBuffer(refFormat, name, &refCsd.first, &refCsd.second);
57         bool hasTestCSD = AMediaFormat_getBuffer(testFormat, name, &testCsd.first, &testCsd.second);
58         if (hasRefCSD != hasTestCSD) {
59             ALOGW("mismatch, ref fmt has CSD %d, test fmt has CSD %d", hasRefCSD, hasTestCSD);
60             return false;
61         }
62         if (hasRefCSD) {
63             if (refCsd.second != testCsd.second) {
64                 ALOGW("ref/test %s buffer sizes are not identical %zu/%zu", name, refCsd.second,
65                       testCsd.second);
66                 return false;
67             }
68             if (memcmp(refCsd.first, testCsd.first, refCsd.second)) {
69                 ALOGW("ref/test %s buffers are not identical", name);
70                 return false;
71             }
72         } else break;
73     }
74     return true;
75 }