blob: ca4dc72a7ae551ada270b84e3e7035fab5a92d8b [file] [log] [blame]
Ray Essick1831f7b2021-03-15 16:10:51 -07001/*
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
31namespace android {
32namespace mediaformatshaper {
33
34//
35// Caller retains ownership of and responsibility for inFormat
36//
37
38//
39// the interface to the outside
40//
41
42int 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
70int 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
85int 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
96 if (!strcmp(feature, "vq-minimum-quality")) {
97 codec->setSupportedMinimumQuality(value);
98 } else if (!strcmp(feature, "vq-supports-qp")) {
99 codec->setSupportsQp(value != 0);
100 } else if (!strcmp(feature, "vq-target-qpmax")) {
101 codec->setTargetQpMax(value);
102 } else if (!strcmp(feature, "vq-target-bppx100")) {
103 double bpp = value / 100.0;
104 codec->setBpp(bpp);
105 } else {
106 // changed nothing, don't mark as configured
107 return 0;
108 }
109 return 0;
110}
111
112/*
113 * The routines that manage finding, creating, and registering the shapers.
114 */
115
116shaperHandle_t findShaper(const char *codecName, const char *mediaType) {
117 CodecProperties *codec = findCodec(codecName, mediaType);
118 return (shaperHandle_t) codec;
119}
120
121shaperHandle_t createShaper(const char *codecName, const char *mediaType) {
122 CodecProperties *codec = new CodecProperties(codecName, mediaType);
123 return (shaperHandle_t) codec;
124}
125
126shaperHandle_t registerShaper(shaperHandle_t shaper, const char *codecName, const char *mediaType) {
127 ALOGV("registerShaper(handle, codecName %s, mediaType %s", codecName, mediaType);
128 CodecProperties *codec = (CodecProperties*) shaper;
129 if (codec == nullptr) {
130 return nullptr;
131 }
132 // must not yet be registered
133 if (codec->isRegistered()) {
134 return nullptr;
135 }
136
137 codec = registerCodec(codec, codecName, mediaType);
138 return (shaperHandle_t) codec;
139}
140
141// mapping & unmapping
142// give me the mappings for 'kind'.
143// kind==null (or empty string), means *all* mappings
144
145const char **getMappings(shaperHandle_t shaper, const char *kind) {
146 CodecProperties *codec = (CodecProperties*) shaper;
147 if (codec == nullptr)
148 return nullptr;
149 if (kind == nullptr)
150 kind = "";
151
152 return codec->getMappings(kind, /* reverse */ false);
153}
154
155const char **getReverseMappings(shaperHandle_t shaper, const char *kind) {
156 CodecProperties *codec = (CodecProperties*) shaper;
157 if (codec == nullptr)
158 return nullptr;
159 if (kind == nullptr)
160 kind = "";
161
162 return codec->getMappings(kind, /* reverse */ true);
163}
164
165
166// the system grabs this structure
167__attribute__ ((visibility ("default")))
168extern "C" FormatShaperOps_t shaper_ops = {
169 .version = SHAPER_VERSION_V1,
170
171 .findShaper = findShaper,
172 .createShaper = createShaper,
173 .setMap = setMap,
174 .setFeature = setFeature,
175 .registerShaper = registerShaper,
176
177 .shapeFormat = shapeFormat,
178 .getMappings = getMappings,
179 .getReverseMappings = getReverseMappings,
180};
181
182} // namespace mediaformatshaper
183} // namespace android
184