Ray Essick | 1831f7b | 2021-03-15 16:10:51 -0700 | [diff] [blame] | 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "FormatShaper" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <string> |
| 22 | #include <inttypes.h> |
| 23 | |
| 24 | #include <media/NdkMediaFormat.h> |
| 25 | |
| 26 | #include <media/formatshaper/VQops.h> |
| 27 | #include <media/formatshaper/CodecProperties.h> |
| 28 | #include <media/formatshaper/FormatShaper.h> |
| 29 | #include <media/formatshaper/VideoShaper.h> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace mediaformatshaper { |
| 33 | |
| 34 | // |
| 35 | // Caller retains ownership of and responsibility for inFormat |
| 36 | // |
| 37 | |
| 38 | // |
| 39 | // the interface to the outside |
| 40 | // |
| 41 | |
| 42 | int shapeFormat(shaperHandle_t shaper, AMediaFormat* inFormat, int flags) { |
| 43 | CodecProperties *codec = (CodecProperties*) shaper; |
| 44 | if (codec == nullptr) { |
| 45 | return -1; |
| 46 | } |
| 47 | if (!codec->isRegistered()) { |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | // run through the list of possible transformations |
| 52 | // |
| 53 | |
| 54 | std::string mediaType = codec->getMediaType(); |
| 55 | if (strncmp(mediaType.c_str(), "video/", 6) == 0) { |
| 56 | // video specific shaping |
| 57 | (void) videoShaper(codec, inFormat, flags); |
| 58 | |
| 59 | } else if (strncmp(mediaType.c_str(), "audio/", 6) == 0) { |
| 60 | // audio specific shaping |
| 61 | |
| 62 | } else { |
| 63 | ALOGV("unknown mediatype '%s', left untouched", mediaType.c_str()); |
| 64 | |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int setMap(shaperHandle_t shaper, const char *kind, const char *key, const char *value) { |
| 71 | ALOGV("setMap: kind %s key %s -> value %s", kind, key, value); |
| 72 | CodecProperties *codec = (CodecProperties*) shaper; |
| 73 | if (codec == nullptr) { |
| 74 | return -1; |
| 75 | } |
| 76 | // must not yet be registered |
| 77 | if (codec->isRegistered()) { |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | codec->setMapping(kind, key, value); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | int setFeature(shaperHandle_t shaper, const char *feature, int value) { |
| 86 | ALOGV("set_feature: feature %s value %d", feature, value); |
| 87 | CodecProperties *codec = (CodecProperties*) shaper; |
| 88 | if (codec == nullptr) { |
| 89 | return -1; |
| 90 | } |
| 91 | // must not yet be registered |
| 92 | if (codec->isRegistered()) { |
| 93 | return -1; |
| 94 | } |
| 95 | |
Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame^] | 96 | // save a map of all features |
| 97 | codec->setFeatureValue(feature, value); |
| 98 | |
Ray Essick | 1831f7b | 2021-03-15 16:10:51 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * The routines that manage finding, creating, and registering the shapers. |
| 104 | */ |
| 105 | |
| 106 | shaperHandle_t findShaper(const char *codecName, const char *mediaType) { |
| 107 | CodecProperties *codec = findCodec(codecName, mediaType); |
| 108 | return (shaperHandle_t) codec; |
| 109 | } |
| 110 | |
| 111 | shaperHandle_t createShaper(const char *codecName, const char *mediaType) { |
| 112 | CodecProperties *codec = new CodecProperties(codecName, mediaType); |
Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame^] | 113 | if (codec != nullptr) { |
| 114 | codec->Seed(); |
| 115 | } |
Ray Essick | 1831f7b | 2021-03-15 16:10:51 -0700 | [diff] [blame] | 116 | return (shaperHandle_t) codec; |
| 117 | } |
| 118 | |
| 119 | shaperHandle_t registerShaper(shaperHandle_t shaper, const char *codecName, const char *mediaType) { |
| 120 | ALOGV("registerShaper(handle, codecName %s, mediaType %s", codecName, mediaType); |
| 121 | CodecProperties *codec = (CodecProperties*) shaper; |
| 122 | if (codec == nullptr) { |
| 123 | return nullptr; |
| 124 | } |
| 125 | // must not yet be registered |
| 126 | if (codec->isRegistered()) { |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame^] | 130 | // any final cleanup for the parameters. This allows us to override |
| 131 | // bad parameters from a devices XML file. |
| 132 | codec->Finish(); |
| 133 | |
| 134 | // may return a different codec, if we lost a race. |
| 135 | // if so, registerCodec() reclaims the one we tried to register for us. |
Ray Essick | 1831f7b | 2021-03-15 16:10:51 -0700 | [diff] [blame] | 136 | codec = registerCodec(codec, codecName, mediaType); |
| 137 | return (shaperHandle_t) codec; |
| 138 | } |
| 139 | |
| 140 | // mapping & unmapping |
| 141 | // give me the mappings for 'kind'. |
| 142 | // kind==null (or empty string), means *all* mappings |
| 143 | |
| 144 | const char **getMappings(shaperHandle_t shaper, const char *kind) { |
| 145 | CodecProperties *codec = (CodecProperties*) shaper; |
| 146 | if (codec == nullptr) |
| 147 | return nullptr; |
| 148 | if (kind == nullptr) |
| 149 | kind = ""; |
| 150 | |
| 151 | return codec->getMappings(kind, /* reverse */ false); |
| 152 | } |
| 153 | |
| 154 | const char **getReverseMappings(shaperHandle_t shaper, const char *kind) { |
| 155 | CodecProperties *codec = (CodecProperties*) shaper; |
| 156 | if (codec == nullptr) |
| 157 | return nullptr; |
| 158 | if (kind == nullptr) |
| 159 | kind = ""; |
| 160 | |
| 161 | return codec->getMappings(kind, /* reverse */ true); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | // the system grabs this structure |
| 166 | __attribute__ ((visibility ("default"))) |
| 167 | extern "C" FormatShaperOps_t shaper_ops = { |
| 168 | .version = SHAPER_VERSION_V1, |
| 169 | |
| 170 | .findShaper = findShaper, |
| 171 | .createShaper = createShaper, |
| 172 | .setMap = setMap, |
| 173 | .setFeature = setFeature, |
| 174 | .registerShaper = registerShaper, |
| 175 | |
| 176 | .shapeFormat = shapeFormat, |
| 177 | .getMappings = getMappings, |
| 178 | .getReverseMappings = getReverseMappings, |
| 179 | }; |
| 180 | |
| 181 | } // namespace mediaformatshaper |
| 182 | } // namespace android |
| 183 | |