1 /*
2 * Copyright (C) 2014 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 "NdkMediaFormat"
19
20 #include <inttypes.h>
21
22 #include <media/NdkMediaFormat.h>
23
24 #include <utils/Log.h>
25 #include <utils/StrongPointer.h>
26 #include <media/stagefright/foundation/ABuffer.h>
27 #include <media/stagefright/foundation/AMessage.h>
28 #include <android_runtime/AndroidRuntime.h>
29 #include <android_util_Binder.h>
30
31 #include <jni.h>
32
33 using namespace android;
34
35 struct AMediaFormat {
36 sp<AMessage> mFormat;
37 String8 mDebug;
38 KeyedVector<String8, String8> mStringCache;
39 };
40
41 extern "C" {
42
43 // private functions for conversion to/from AMessage
AMediaFormat_fromMsg(const void * data)44 AMediaFormat* AMediaFormat_fromMsg(const void* data) {
45 ALOGV("private ctor");
46 AMediaFormat* mData = new AMediaFormat();
47 mData->mFormat = *((sp<AMessage>*)data);
48 if (mData->mFormat == NULL) {
49 ALOGW("got NULL format");
50 mData->mFormat = new AMessage;
51 }
52 return mData;
53 }
54
AMediaFormat_getFormat(const AMediaFormat * mData,void * dest)55 void AMediaFormat_getFormat(const AMediaFormat* mData, void* dest) {
56 *((sp<AMessage>*)dest) = mData->mFormat;
57 }
58
59
60 /*
61 * public function follow
62 */
63 EXPORT
AMediaFormat_new()64 AMediaFormat *AMediaFormat_new() {
65 ALOGV("ctor");
66 sp<AMessage> msg = new AMessage();
67 return AMediaFormat_fromMsg(&msg);
68 }
69
70 EXPORT
AMediaFormat_delete(AMediaFormat * mData)71 media_status_t AMediaFormat_delete(AMediaFormat *mData) {
72 ALOGV("dtor");
73 delete mData;
74 return AMEDIA_OK;
75 }
76
77
78 EXPORT
AMediaFormat_toString(AMediaFormat * mData)79 const char* AMediaFormat_toString(AMediaFormat *mData) {
80 sp<AMessage> f = mData->mFormat;
81 String8 ret;
82 int num = f->countEntries();
83 for (int i = 0; i < num; i++) {
84 if (i != 0) {
85 ret.append(", ");
86 }
87 AMessage::Type t;
88 const char *name = f->getEntryNameAt(i, &t);
89 ret.append(name);
90 ret.append(": ");
91 switch (t) {
92 case AMessage::kTypeInt32:
93 {
94 int32_t val;
95 f->findInt32(name, &val);
96 ret.appendFormat("int32(%" PRId32 ")", val);
97 break;
98 }
99 case AMessage::kTypeInt64:
100 {
101 int64_t val;
102 f->findInt64(name, &val);
103 ret.appendFormat("int64(%" PRId64 ")", val);
104 break;
105 }
106 case AMessage::kTypeSize:
107 {
108 size_t val;
109 f->findSize(name, &val);
110 ret.appendFormat("size_t(%zu)", val);
111 break;
112 }
113 case AMessage::kTypeFloat:
114 {
115 float val;
116 f->findFloat(name, &val);
117 ret.appendFormat("float(%f)", val);
118 break;
119 }
120 case AMessage::kTypeDouble:
121 {
122 double val;
123 f->findDouble(name, &val);
124 ret.appendFormat("double(%f)", val);
125 break;
126 }
127 case AMessage::kTypeRect:
128 {
129 int32_t left, top, right, bottom;
130 f->findRect(name, &left, &top, &right, &bottom);
131 ret.appendFormat("Rect(%" PRId32 ", %" PRId32 ", %" PRId32 ", %" PRId32 ")",
132 left, top, right, bottom);
133 break;
134 }
135 case AMessage::kTypeString:
136 {
137 AString val;
138 f->findString(name, &val);
139 ret.appendFormat("string(%s)", val.c_str());
140 break;
141 }
142 case AMessage::kTypeBuffer:
143 {
144 ret.appendFormat("data");
145 break;
146 }
147 default:
148 {
149 ret.appendFormat("unknown(%d)", t);
150 break;
151 }
152 }
153 }
154 ret.append("}");
155 mData->mDebug = ret;
156 return mData->mDebug.string();
157 }
158
159 EXPORT
AMediaFormat_getInt32(AMediaFormat * format,const char * name,int32_t * out)160 bool AMediaFormat_getInt32(AMediaFormat* format, const char *name, int32_t *out) {
161 return format->mFormat->findInt32(name, out);
162 }
163
164 EXPORT
AMediaFormat_getInt64(AMediaFormat * format,const char * name,int64_t * out)165 bool AMediaFormat_getInt64(AMediaFormat* format, const char *name, int64_t *out) {
166 return format->mFormat->findInt64(name, out);
167 }
168
169 EXPORT
AMediaFormat_getFloat(AMediaFormat * format,const char * name,float * out)170 bool AMediaFormat_getFloat(AMediaFormat* format, const char *name, float *out) {
171 return format->mFormat->findFloat(name, out);
172 }
173
174 EXPORT
AMediaFormat_getDouble(AMediaFormat * format,const char * name,double * out)175 bool AMediaFormat_getDouble(AMediaFormat* format, const char *name, double *out) {
176 return format->mFormat->findDouble(name, out);
177 }
178
179 EXPORT
AMediaFormat_getSize(AMediaFormat * format,const char * name,size_t * out)180 bool AMediaFormat_getSize(AMediaFormat* format, const char *name, size_t *out) {
181 return format->mFormat->findSize(name, out);
182 }
183
184 EXPORT
AMediaFormat_getRect(AMediaFormat * format,const char * name,int32_t * left,int32_t * top,int32_t * right,int32_t * bottom)185 bool AMediaFormat_getRect(AMediaFormat* format, const char *name,
186 int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) {
187 return format->mFormat->findRect(name, left, top, right, bottom);
188 }
189
190 EXPORT
AMediaFormat_getBuffer(AMediaFormat * format,const char * name,void ** data,size_t * outsize)191 bool AMediaFormat_getBuffer(AMediaFormat* format, const char *name, void** data, size_t *outsize) {
192 sp<ABuffer> buf;
193 if (format->mFormat->findBuffer(name, &buf)) {
194 *data = buf->data() + buf->offset();
195 *outsize = buf->size();
196 return true;
197 }
198 return false;
199 }
200
201 EXPORT
AMediaFormat_getString(AMediaFormat * mData,const char * name,const char ** out)202 bool AMediaFormat_getString(AMediaFormat* mData, const char *name, const char **out) {
203
204 for (size_t i = 0; i < mData->mStringCache.size(); i++) {
205 if (strcmp(mData->mStringCache.keyAt(i).string(), name) == 0) {
206 mData->mStringCache.removeItemsAt(i, 1);
207 break;
208 }
209 }
210
211 AString tmp;
212 if (mData->mFormat->findString(name, &tmp)) {
213 String8 ret(tmp.c_str());
214 mData->mStringCache.add(String8(name), ret);
215 *out = ret.string();
216 return true;
217 }
218 return false;
219 }
220
221 EXPORT
AMediaFormat_setInt32(AMediaFormat * format,const char * name,int32_t value)222 void AMediaFormat_setInt32(AMediaFormat* format, const char *name, int32_t value) {
223 format->mFormat->setInt32(name, value);
224 }
225
226 EXPORT
AMediaFormat_setInt64(AMediaFormat * format,const char * name,int64_t value)227 void AMediaFormat_setInt64(AMediaFormat* format, const char *name, int64_t value) {
228 format->mFormat->setInt64(name, value);
229 }
230
231 EXPORT
AMediaFormat_setFloat(AMediaFormat * format,const char * name,float value)232 void AMediaFormat_setFloat(AMediaFormat* format, const char* name, float value) {
233 format->mFormat->setFloat(name, value);
234 }
235
236 EXPORT
AMediaFormat_setDouble(AMediaFormat * format,const char * name,double value)237 void AMediaFormat_setDouble(AMediaFormat* format, const char* name, double value) {
238 format->mFormat->setDouble(name, value);
239 }
240
241 EXPORT
AMediaFormat_setSize(AMediaFormat * format,const char * name,size_t value)242 void AMediaFormat_setSize(AMediaFormat* format, const char* name, size_t value) {
243 format->mFormat->setSize(name, value);
244 }
245
246 EXPORT
AMediaFormat_setRect(AMediaFormat * format,const char * name,int32_t left,int32_t top,int32_t right,int32_t bottom)247 void AMediaFormat_setRect(AMediaFormat* format, const char *name,
248 int32_t left, int32_t top, int32_t right, int32_t bottom) {
249 format->mFormat->setRect(name, left, top, right, bottom);
250 }
251
252 EXPORT
AMediaFormat_setString(AMediaFormat * format,const char * name,const char * value)253 void AMediaFormat_setString(AMediaFormat* format, const char* name, const char* value) {
254 // AMessage::setString() makes a copy of the string
255 format->mFormat->setString(name, value, strlen(value));
256 }
257
258 EXPORT
AMediaFormat_setBuffer(AMediaFormat * format,const char * name,void * data,size_t size)259 void AMediaFormat_setBuffer(AMediaFormat* format, const char* name, void* data, size_t size) {
260 // the ABuffer(void*, size_t) constructor doesn't take ownership of the data, so create
261 // a new buffer and copy the data into it
262 sp<ABuffer> buf = new ABuffer(size);
263 memcpy(buf->data(), data, size);
264 buf->setRange(0, size);
265 // AMessage::setBuffer() increases the refcount of the buffer
266 format->mFormat->setBuffer(name, buf);
267 }
268
269
270 EXPORT const char* AMEDIAFORMAT_KEY_AAC_DRC_ATTENUATION_FACTOR = "aac-drc-cut-level";
271 EXPORT const char* AMEDIAFORMAT_KEY_AAC_DRC_BOOST_FACTOR = "aac-drc-boost-level";
272 EXPORT const char* AMEDIAFORMAT_KEY_AAC_DRC_HEAVY_COMPRESSION = "aac-drc-heavy-compression";
273 EXPORT const char* AMEDIAFORMAT_KEY_AAC_DRC_TARGET_REFERENCE_LEVEL = "aac-target-ref-level";
274 EXPORT const char* AMEDIAFORMAT_KEY_AAC_ENCODED_TARGET_LEVEL = "aac-encoded-target-level";
275 EXPORT const char* AMEDIAFORMAT_KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT = "aac-max-output-channel_count";
276 EXPORT const char* AMEDIAFORMAT_KEY_AAC_PROFILE = "aac-profile";
277 EXPORT const char* AMEDIAFORMAT_KEY_AAC_SBR_MODE = "aac-sbr-mode";
278 EXPORT const char* AMEDIAFORMAT_KEY_AUDIO_SESSION_ID = "audio-session-id";
279 EXPORT const char* AMEDIAFORMAT_KEY_BITRATE_MODE = "bitrate-mode";
280 EXPORT const char* AMEDIAFORMAT_KEY_BIT_RATE = "bitrate";
281 EXPORT const char* AMEDIAFORMAT_KEY_CAPTURE_RATE = "capture-rate";
282 EXPORT const char* AMEDIAFORMAT_KEY_CHANNEL_COUNT = "channel-count";
283 EXPORT const char* AMEDIAFORMAT_KEY_CHANNEL_MASK = "channel-mask";
284 EXPORT const char* AMEDIAFORMAT_KEY_COLOR_FORMAT = "color-format";
285 EXPORT const char* AMEDIAFORMAT_KEY_COLOR_RANGE = "color-range";
286 EXPORT const char* AMEDIAFORMAT_KEY_COLOR_STANDARD = "color-standard";
287 EXPORT const char* AMEDIAFORMAT_KEY_COLOR_TRANSFER = "color-transfer";
288 EXPORT const char* AMEDIAFORMAT_KEY_COMPLEXITY = "complexity";
289 EXPORT const char* AMEDIAFORMAT_KEY_CSD = "csd";
290 EXPORT const char* AMEDIAFORMAT_KEY_CSD_0 = "csd-0";
291 EXPORT const char* AMEDIAFORMAT_KEY_CSD_1 = "csd-1";
292 EXPORT const char* AMEDIAFORMAT_KEY_CSD_2 = "csd-2";
293 EXPORT const char* AMEDIAFORMAT_KEY_DISPLAY_CROP = "crop";
294 EXPORT const char* AMEDIAFORMAT_KEY_DISPLAY_HEIGHT = "display-height";
295 EXPORT const char* AMEDIAFORMAT_KEY_DISPLAY_WIDTH = "display-width";
296 EXPORT const char* AMEDIAFORMAT_KEY_DURATION = "durationUs";
297 EXPORT const char* AMEDIAFORMAT_KEY_FLAC_COMPRESSION_LEVEL = "flac-compression-level";
298 EXPORT const char* AMEDIAFORMAT_KEY_FRAME_RATE = "frame-rate";
299 EXPORT const char* AMEDIAFORMAT_KEY_GRID_COLUMNS = "grid-cols";
300 EXPORT const char* AMEDIAFORMAT_KEY_GRID_ROWS = "grid-rows";
301 EXPORT const char* AMEDIAFORMAT_KEY_HDR_STATIC_INFO = "hdr-static-info";
302 EXPORT const char* AMEDIAFORMAT_KEY_HEIGHT = "height";
303 EXPORT const char* AMEDIAFORMAT_KEY_INTRA_REFRESH_PERIOD = "intra-refresh-period";
304 EXPORT const char* AMEDIAFORMAT_KEY_IS_ADTS = "is-adts";
305 EXPORT const char* AMEDIAFORMAT_KEY_IS_AUTOSELECT = "is-autoselect";
306 EXPORT const char* AMEDIAFORMAT_KEY_IS_DEFAULT = "is-default";
307 EXPORT const char* AMEDIAFORMAT_KEY_IS_FORCED_SUBTITLE = "is-forced-subtitle";
308 EXPORT const char* AMEDIAFORMAT_KEY_I_FRAME_INTERVAL = "i-frame-interval";
309 EXPORT const char* AMEDIAFORMAT_KEY_LANGUAGE = "language";
310 EXPORT const char* AMEDIAFORMAT_KEY_LATENCY = "latency";
311 EXPORT const char* AMEDIAFORMAT_KEY_LEVEL = "level";
312 EXPORT const char* AMEDIAFORMAT_KEY_MAX_HEIGHT = "max-height";
313 EXPORT const char* AMEDIAFORMAT_KEY_MAX_INPUT_SIZE = "max-input-size";
314 EXPORT const char* AMEDIAFORMAT_KEY_MAX_WIDTH = "max-width";
315 EXPORT const char* AMEDIAFORMAT_KEY_MIME = "mime";
316 EXPORT const char* AMEDIAFORMAT_KEY_MPEG_USER_DATA = "mpeg-user-data";
317 EXPORT const char* AMEDIAFORMAT_KEY_OPERATING_RATE = "operating-rate";
318 EXPORT const char* AMEDIAFORMAT_KEY_PCM_ENCODING = "pcm-encoding";
319 EXPORT const char* AMEDIAFORMAT_KEY_PRIORITY = "priority";
320 EXPORT const char* AMEDIAFORMAT_KEY_PROFILE = "profile";
321 EXPORT const char* AMEDIAFORMAT_KEY_PUSH_BLANK_BUFFERS_ON_STOP = "push-blank-buffers-on-shutdown";
322 EXPORT const char* AMEDIAFORMAT_KEY_REPEAT_PREVIOUS_FRAME_AFTER = "repeat-previous-frame-after";
323 EXPORT const char* AMEDIAFORMAT_KEY_ROTATION = "rotation-degrees";
324 EXPORT const char* AMEDIAFORMAT_KEY_SAMPLE_RATE = "sample-rate";
325 EXPORT const char* AMEDIAFORMAT_KEY_SEI = "sei";
326 EXPORT const char* AMEDIAFORMAT_KEY_SLICE_HEIGHT = "slice-height";
327 EXPORT const char* AMEDIAFORMAT_KEY_STRIDE = "stride";
328 EXPORT const char* AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID = "temporal-layer-id";
329 EXPORT const char* AMEDIAFORMAT_KEY_TEMPORAL_LAYERING = "ts-schema";
330 EXPORT const char* AMEDIAFORMAT_KEY_TILE_HEIGHT = "tile-height";
331 EXPORT const char* AMEDIAFORMAT_KEY_TILE_WIDTH = "tile-width";
332 EXPORT const char* AMEDIAFORMAT_KEY_TIME_US = "timeUs";
333 EXPORT const char* AMEDIAFORMAT_KEY_TRACK_ID = "track-id";
334 EXPORT const char* AMEDIAFORMAT_KEY_TRACK_INDEX = "track-index";
335 EXPORT const char* AMEDIAFORMAT_KEY_WIDTH = "width";
336
337
338 } // extern "C"
339
340
341