| Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 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" | 
| Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 18 |  | 
| Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> | 
| Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 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) */ | 
| Linus Nilsson | 9359189 | 2020-08-03 18:56:55 -0700 | [diff] [blame] | 38 | const char* TBD_AMEDIACODEC_PARAMETER_KEY_ALLOW_FRAME_DROP = "allow-frame-drop"; | 
| Chong Zhang | 6646927 | 2020-06-04 16:51:55 -0700 | [diff] [blame] | 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"; | 
| Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 42 |  | 
| Linus Nilsson | f464154 | 2021-02-04 15:12:26 -0800 | [diff] [blame] | 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 |  | 
| Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 46 | namespace AMediaFormatUtils { | 
 | 47 |  | 
 | 48 | #define DEFINE_FORMAT_VALUE_COPY_FUNC(_type, _typeName)                                      \ | 
 | 49 |     bool CopyFormatEntry##_typeName(const char* key, AMediaFormat* from, AMediaFormat* to) { \ | 
 | 50 |         _type value;                                                                         \ | 
 | 51 |         if (AMediaFormat_get##_typeName(from, key, &value)) {                                \ | 
 | 52 |             AMediaFormat_set##_typeName(to, key, value);                                     \ | 
 | 53 |             return true;                                                                     \ | 
 | 54 |         }                                                                                    \ | 
 | 55 |         return false;                                                                        \ | 
 | 56 |     } | 
 | 57 |  | 
 | 58 | DEFINE_FORMAT_VALUE_COPY_FUNC(const char*, String); | 
 | 59 | DEFINE_FORMAT_VALUE_COPY_FUNC(int64_t, Int64); | 
 | 60 | DEFINE_FORMAT_VALUE_COPY_FUNC(int32_t, Int32); | 
 | 61 | DEFINE_FORMAT_VALUE_COPY_FUNC(float, Float); | 
 | 62 |  | 
 | 63 | void CopyFormatEntries(AMediaFormat* from, AMediaFormat* to, const EntryCopier* entries, | 
 | 64 |                        size_t entryCount) { | 
 | 65 |     if (from == nullptr || to == nullptr) { | 
 | 66 |         LOG(ERROR) << "Cannot copy null formats"; | 
 | 67 |         return; | 
 | 68 |     } else if (entries == nullptr || entryCount < 1) { | 
 | 69 |         LOG(WARNING) << "No entries to copy"; | 
 | 70 |         return; | 
 | 71 |     } | 
 | 72 |  | 
 | 73 |     for (size_t i = 0; i < entryCount; ++i) { | 
 | 74 |         if (!entries[i].copy(entries[i].key, from, to) && entries[i].copy2 != nullptr) { | 
 | 75 |             entries[i].copy2(entries[i].key, from, to); | 
 | 76 |         } | 
 | 77 |     } | 
 | 78 | } | 
| Linus Nilsson | 7a127b2 | 2020-10-15 16:23:54 -0700 | [diff] [blame] | 79 |  | 
 | 80 | #define DEFINE_SET_DEFAULT_FORMAT_VALUE_FUNC(_type, _typeName)                                  \ | 
 | 81 |     bool SetDefaultFormatValue##_typeName(const char* key, AMediaFormat* format, _type value) { \ | 
 | 82 |         _type tmp;                                                                              \ | 
 | 83 |         if (!AMediaFormat_get##_typeName(format, key, &tmp)) {                                  \ | 
 | 84 |             AMediaFormat_set##_typeName(format, key, value);                                    \ | 
 | 85 |             return true;                                                                        \ | 
 | 86 |         }                                                                                       \ | 
 | 87 |         return false;                                                                           \ | 
 | 88 |     } | 
 | 89 |  | 
 | 90 | DEFINE_SET_DEFAULT_FORMAT_VALUE_FUNC(float, Float); | 
 | 91 | DEFINE_SET_DEFAULT_FORMAT_VALUE_FUNC(int32_t, Int32); | 
 | 92 |  | 
| Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 93 | }  // namespace AMediaFormatUtils |