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 | |
Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 102 | int setTuning(shaperHandle_t shaper, const char *tuning, const char *value) { |
| 103 | ALOGV("setTuning: tuning %s value %s", tuning, value); |
| 104 | CodecProperties *codec = (CodecProperties*) shaper; |
| 105 | if (codec == nullptr) { |
| 106 | return -1; |
| 107 | } |
| 108 | // must not yet be registered |
| 109 | if (codec->isRegistered()) { |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | // save a map of all features |
| 114 | codec->setTuningValue(tuning, value); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Ray Essick | 1831f7b | 2021-03-15 16:10:51 -0700 | [diff] [blame] | 119 | /* |
| 120 | * The routines that manage finding, creating, and registering the shapers. |
| 121 | */ |
| 122 | |
| 123 | shaperHandle_t findShaper(const char *codecName, const char *mediaType) { |
| 124 | CodecProperties *codec = findCodec(codecName, mediaType); |
| 125 | return (shaperHandle_t) codec; |
| 126 | } |
| 127 | |
| 128 | shaperHandle_t createShaper(const char *codecName, const char *mediaType) { |
| 129 | CodecProperties *codec = new CodecProperties(codecName, mediaType); |
Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 130 | if (codec != nullptr) { |
| 131 | codec->Seed(); |
| 132 | } |
Ray Essick | 1831f7b | 2021-03-15 16:10:51 -0700 | [diff] [blame] | 133 | return (shaperHandle_t) codec; |
| 134 | } |
| 135 | |
| 136 | shaperHandle_t registerShaper(shaperHandle_t shaper, const char *codecName, const char *mediaType) { |
| 137 | ALOGV("registerShaper(handle, codecName %s, mediaType %s", codecName, mediaType); |
| 138 | CodecProperties *codec = (CodecProperties*) shaper; |
| 139 | if (codec == nullptr) { |
| 140 | return nullptr; |
| 141 | } |
| 142 | // must not yet be registered |
| 143 | if (codec->isRegistered()) { |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 147 | // any final cleanup for the parameters. This allows us to override |
| 148 | // bad parameters from a devices XML file. |
| 149 | codec->Finish(); |
| 150 | |
| 151 | // may return a different codec, if we lost a race. |
| 152 | // if so, registerCodec() reclaims the one we tried to register for us. |
Ray Essick | 1831f7b | 2021-03-15 16:10:51 -0700 | [diff] [blame] | 153 | codec = registerCodec(codec, codecName, mediaType); |
| 154 | return (shaperHandle_t) codec; |
| 155 | } |
| 156 | |
| 157 | // mapping & unmapping |
| 158 | // give me the mappings for 'kind'. |
| 159 | // kind==null (or empty string), means *all* mappings |
| 160 | |
| 161 | const char **getMappings(shaperHandle_t shaper, const char *kind) { |
| 162 | CodecProperties *codec = (CodecProperties*) shaper; |
| 163 | if (codec == nullptr) |
| 164 | return nullptr; |
| 165 | if (kind == nullptr) |
| 166 | kind = ""; |
| 167 | |
| 168 | return codec->getMappings(kind, /* reverse */ false); |
| 169 | } |
| 170 | |
| 171 | const char **getReverseMappings(shaperHandle_t shaper, const char *kind) { |
| 172 | CodecProperties *codec = (CodecProperties*) shaper; |
| 173 | if (codec == nullptr) |
| 174 | return nullptr; |
| 175 | if (kind == nullptr) |
| 176 | kind = ""; |
| 177 | |
| 178 | return codec->getMappings(kind, /* reverse */ true); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | // the system grabs this structure |
| 183 | __attribute__ ((visibility ("default"))) |
| 184 | extern "C" FormatShaperOps_t shaper_ops = { |
| 185 | .version = SHAPER_VERSION_V1, |
| 186 | |
| 187 | .findShaper = findShaper, |
| 188 | .createShaper = createShaper, |
| 189 | .setMap = setMap, |
| 190 | .setFeature = setFeature, |
| 191 | .registerShaper = registerShaper, |
| 192 | |
| 193 | .shapeFormat = shapeFormat, |
| 194 | .getMappings = getMappings, |
| 195 | .getReverseMappings = getReverseMappings, |
Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 196 | |
| 197 | .setTuning = setTuning, |
Ray Essick | 1831f7b | 2021-03-15 16:10:51 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | } // namespace mediaformatshaper |
| 201 | } // namespace android |
| 202 | |