1 /*
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 //#define LOG_NDEBUG 0
17 #define LOG_TAG "NdkCommon"
18
19 #include <android-base/logging.h>
20 #include <media/NdkCommon.h>
21
22 #include <cstdio>
23 #include <cstring>
24 #include <utility>
25
26 /* TODO(b/153592281)
27 * Note: constants used by the native media tests but not available in media ndk api
28 */
29 const char* AMEDIA_MIMETYPE_VIDEO_VP8 = "video/x-vnd.on2.vp8";
30 const char* AMEDIA_MIMETYPE_VIDEO_VP9 = "video/x-vnd.on2.vp9";
31 const char* AMEDIA_MIMETYPE_VIDEO_AV1 = "video/av01";
32 const char* AMEDIA_MIMETYPE_VIDEO_AVC = "video/avc";
33 const char* AMEDIA_MIMETYPE_VIDEO_HEVC = "video/hevc";
34 const char* AMEDIA_MIMETYPE_VIDEO_MPEG4 = "video/mp4v-es";
35 const char* AMEDIA_MIMETYPE_VIDEO_H263 = "video/3gpp";
36
37 /* TODO(b/153592281) */
38 const char* TBD_AMEDIACODEC_PARAMETER_KEY_ALLOW_FRAME_DROP = "allow-frame-drop";
39 const char* TBD_AMEDIACODEC_PARAMETER_KEY_REQUEST_SYNC_FRAME = "request-sync";
40 const char* TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE = "video-bitrate";
41 const char* TBD_AMEDIACODEC_PARAMETER_KEY_MAX_B_FRAMES = "max-bframes";
42
43 /* TODO(lnilsson): Finalize value or adopt AMediaFormat key once available. */
44 const char* TBD_AMEDIACODEC_PARAMETER_KEY_COLOR_TRANSFER_REQUEST = "color-transfer-request";
45 const char* TBD_AMEDIACODEC_PARAMETER_KEY_BACKGROUND_MODE = "android._background-mode";
46
47 namespace AMediaFormatUtils {
48
49 #define DEFINE_FORMAT_VALUE_COPY_FUNC(_type, _typeName) \
50 bool CopyFormatEntry##_typeName(const char* key, AMediaFormat* from, AMediaFormat* to) { \
51 _type value; \
52 if (AMediaFormat_get##_typeName(from, key, &value)) { \
53 AMediaFormat_set##_typeName(to, key, value); \
54 return true; \
55 } \
56 return false; \
57 }
58
59 DEFINE_FORMAT_VALUE_COPY_FUNC(const char*, String);
60 DEFINE_FORMAT_VALUE_COPY_FUNC(int64_t, Int64);
61 DEFINE_FORMAT_VALUE_COPY_FUNC(int32_t, Int32);
62 DEFINE_FORMAT_VALUE_COPY_FUNC(float, Float);
63
CopyFormatEntries(AMediaFormat * from,AMediaFormat * to,const std::vector<EntryCopier> & entries)64 void CopyFormatEntries(AMediaFormat* from, AMediaFormat* to,
65 const std::vector<EntryCopier>& entries) {
66 if (from == nullptr || to == nullptr) {
67 LOG(ERROR) << "Cannot copy null formats";
68 return;
69 } else if (entries.empty()) {
70 LOG(WARNING) << "No entries to copy";
71 return;
72 }
73
74 for (auto& entry : entries) {
75 if (!entry.copy(entry.key, from, to) && entry.copy2 != nullptr) {
76 entry.copy2(entry.key, from, to);
77 }
78 }
79 }
80
81 #define DEFINE_SET_DEFAULT_FORMAT_VALUE_FUNC(_type, _typeName) \
82 bool SetDefaultFormatValue##_typeName(const char* key, AMediaFormat* format, _type value) { \
83 _type tmp; \
84 if (!AMediaFormat_get##_typeName(format, key, &tmp)) { \
85 AMediaFormat_set##_typeName(format, key, value); \
86 return true; \
87 } \
88 return false; \
89 }
90
91 DEFINE_SET_DEFAULT_FORMAT_VALUE_FUNC(float, Float);
92 DEFINE_SET_DEFAULT_FORMAT_VALUE_FUNC(int32_t, Int32);
93
94 // Determines whether a track format describes HDR video content or not. The
95 // logic is based on isHdr() in libstagefright/Utils.cpp.
VideoIsHdr(AMediaFormat * format)96 bool VideoIsHdr(AMediaFormat* format) {
97 // If VUI signals HDR content, this internal flag is set by the extractor.
98 int32_t isHdr;
99 if (AMediaFormat_getInt32(format, "android._is-hdr", &isHdr)) {
100 return isHdr;
101 }
102
103 // If container supplied HDR static info without transfer set, assume HDR.
104 const char* hdrInfo;
105 int32_t transfer;
106 if ((AMediaFormat_getString(format, AMEDIAFORMAT_KEY_HDR_STATIC_INFO, &hdrInfo) ||
107 AMediaFormat_getString(format, "hdr10-plus-info", &hdrInfo)) &&
108 !AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_COLOR_TRANSFER, &transfer)) {
109 return true;
110 }
111
112 // Otherwise, check if an HDR transfer function is set.
113 if (AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_COLOR_TRANSFER, &transfer)) {
114 return transfer == COLOR_TRANSFER_ST2084 || transfer == COLOR_TRANSFER_HLG;
115 }
116
117 return false;
118 }
119 } // namespace AMediaFormatUtils
120