blob: 42502e0f80168f19e8cbe361634a5e45b80707e0 [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
Ray Essick970f1c82021-03-25 13:37:45 -070096 // save a map of all features
97 codec->setFeatureValue(feature, value);
98
Ray Essick1831f7b2021-03-15 16:10:51 -070099 return 0;
100}
101
Ray Essicka727ef92021-03-29 13:35:02 -0700102int 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 Essick1831f7b2021-03-15 16:10:51 -0700119/*
120 * The routines that manage finding, creating, and registering the shapers.
121 */
122
123shaperHandle_t findShaper(const char *codecName, const char *mediaType) {
124 CodecProperties *codec = findCodec(codecName, mediaType);
125 return (shaperHandle_t) codec;
126}
127
128shaperHandle_t createShaper(const char *codecName, const char *mediaType) {
129 CodecProperties *codec = new CodecProperties(codecName, mediaType);
Ray Essick970f1c82021-03-25 13:37:45 -0700130 if (codec != nullptr) {
131 codec->Seed();
132 }
Ray Essick1831f7b2021-03-15 16:10:51 -0700133 return (shaperHandle_t) codec;
134}
135
136shaperHandle_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 Essick970f1c82021-03-25 13:37:45 -0700147 // 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 Essick1831f7b2021-03-15 16:10:51 -0700153 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
161const 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
171const 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")))
184extern "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 Essicka727ef92021-03-29 13:35:02 -0700196
197 .setTuning = setTuning,
Ray Essick1831f7b2021-03-15 16:10:51 -0700198};
199
200} // namespace mediaformatshaper
201} // namespace android
202