| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 "CodecSeeding" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <string> |
| 22 | |
| Ray Essick | 7b4e9d9 | 2021-04-20 16:48:00 -0700 | [diff] [blame^] | 23 | #include "CodecProperties.h" |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace mediaformatshaper { |
| 27 | |
| 28 | /* |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 29 | * a block of pre-loaded tunings for codecs. |
| 30 | * |
| 31 | * things the library seeds into the codecproperties based |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 32 | * on the mediaType. |
| 33 | * XXX: parsing from a file is likely better than embedding in code. |
| 34 | */ |
| 35 | typedef struct { |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 36 | bool overrideable; |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 37 | const char *key; |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 38 | const char *value; |
| 39 | } preloadTuning_t; |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 40 | |
| 41 | typedef struct { |
| 42 | const char *mediaType; |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 43 | preloadTuning_t *features; |
| 44 | } preloadTunings_t; |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * 240 = 2.4 bits per pixel-per-second == 5mbps@1080, 2.3mbps@720p, which is about where |
| 48 | * we want our initial floor for now. |
| 49 | */ |
| 50 | |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 51 | static preloadTuning_t featuresAvc[] = { |
| Ray Essick | 7b4e9d9 | 2021-04-20 16:48:00 -0700 | [diff] [blame^] | 52 | // {true, "vq-target-bpp", "2.45"}, |
| Ray Essick | 244aaa5 | 2021-04-05 14:47:02 -0700 | [diff] [blame] | 53 | {true, "vq-target-bpp-1080p", "2.40"}, |
| 54 | {true, "vq-target-bpp-540p", "2.60"}, |
| 55 | {true, "vq-target-bpp-480p", "3.00"}, |
| 56 | {true, "vq-target-qpmax", "40"}, |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 57 | {true, nullptr, 0} |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 60 | static preloadTuning_t featuresHevc[] = { |
| Ray Essick | 7b4e9d9 | 2021-04-20 16:48:00 -0700 | [diff] [blame^] | 61 | // {true, "vq-target-bpp", "1.80"}, |
| 62 | {true, "vq-target-bpp-1080p", "1.50"}, |
| 63 | {true, "vq-target-bpp-720p", "1.80"}, |
| 64 | {true, "vq-target-bpp-540p", "2.10"}, |
| 65 | // no qp for hevc, at least for now |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 66 | {true, nullptr, 0} |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 69 | static preloadTuning_t featuresGenericVideo[] = { |
| 70 | {true, "vq-target-bpp", "2.40"}, |
| 71 | {true, nullptr, 0} |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 74 | static preloadTunings_t preloadTunings[] = { |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 75 | { "video/avc", featuresAvc}, |
| 76 | { "video/hevc", &featuresHevc[0]}, |
| 77 | |
| 78 | // wildcard for any video format not already captured |
| 79 | { "video/*", &featuresGenericVideo[0]}, |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 80 | |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 81 | { nullptr, nullptr} |
| 82 | }; |
| 83 | |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 84 | void CodecProperties::addMediaDefaults(bool overrideable) { |
| 85 | ALOGD("Seed: codec %s, mediatype %s, overrideable %d", |
| 86 | mName.c_str(), mMediaType.c_str(), overrideable); |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 87 | |
| 88 | // load me up with initial configuration data |
| 89 | int count = 0; |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 90 | for (int i = 0; ; i++) { |
| 91 | preloadTunings_t *p = &preloadTunings[i]; |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 92 | if (p->mediaType == nullptr) { |
| 93 | break; |
| 94 | } |
| 95 | bool found = false; |
| 96 | if (strcmp(p->mediaType, mMediaType.c_str()) == 0) { |
| 97 | found = true; |
| 98 | } |
| 99 | const char *r; |
| 100 | if (!found && (r = strchr(p->mediaType, '*')) != NULL) { |
| 101 | // wildcard; check the prefix |
| 102 | size_t len = r - p->mediaType; |
| 103 | if (strncmp(p->mediaType, mMediaType.c_str(), len) == 0) { |
| 104 | found = true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (!found) { |
| 109 | continue; |
| 110 | } |
| 111 | ALOGV("seeding from mediaType '%s'", p->mediaType); |
| 112 | |
| 113 | // walk through, filling things |
| 114 | if (p->features != nullptr) { |
| 115 | for (int j=0;; j++) { |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 116 | preloadTuning_t *q = &p->features[j]; |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 117 | if (q->key == nullptr) { |
| 118 | break; |
| 119 | } |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 120 | if (q->overrideable != overrideable) { |
| 121 | continue; |
| 122 | } |
| 123 | setTuningValue(q->key, q->value); |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 124 | count++; |
| 125 | } |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | ALOGV("loaded %d preset values", count); |
| 130 | } |
| 131 | |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 132 | // a chance, as we create the codec to inject any default behaviors we want. |
| 133 | // XXX: consider whether we need pre/post or just post. it affects what can be |
| 134 | // overridden by way of the codec XML |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 135 | // |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 136 | void CodecProperties::Seed() { |
| 137 | ALOGV("Seed: for codec %s, mediatype %s", mName.c_str(), mMediaType.c_str()); |
| 138 | addMediaDefaults(true); |
| 139 | } |
| 140 | |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 141 | void CodecProperties::Finish() { |
| 142 | ALOGV("Finish: for codec %s, mediatype %s", mName.c_str(), mMediaType.c_str()); |
| Ray Essick | a727ef9 | 2021-03-29 13:35:02 -0700 | [diff] [blame] | 143 | addMediaDefaults(false); |
| Ray Essick | 970f1c8 | 2021-03-25 13:37:45 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } // namespace mediaformatshaper |
| 147 | } // namespace android |
| 148 | |