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 #include <media/NdkMediaFormatPriv.h>
24
25 #include <utils/Log.h>
26 #include <utils/StrongPointer.h>
27 #include <media/stagefright/foundation/ABuffer.h>
28 #include <media/stagefright/foundation/AMessage.h>
29 #include <android_util_Binder.h>
30
31 #include <jni.h>
32
33 using namespace android;
34
35 extern "C" {
36
37 /*
38 * public function follow
39 */
40 EXPORT
AMediaFormat_new()41 AMediaFormat *AMediaFormat_new() {
42 ALOGV("ctor");
43 sp<AMessage> msg = new AMessage();
44 return AMediaFormat_fromMsg(&msg);
45 }
46
47 EXPORT
AMediaFormat_delete(AMediaFormat * mData)48 media_status_t AMediaFormat_delete(AMediaFormat *mData) {
49 ALOGV("dtor");
50 delete mData;
51 return AMEDIA_OK;
52 }
53
54 EXPORT
AMediaFormat_clear(AMediaFormat * format)55 void AMediaFormat_clear(AMediaFormat *format) {
56 format->mFormat->clear();
57 }
58
59 EXPORT
AMediaFormat_copy(AMediaFormat * to,AMediaFormat * from)60 media_status_t AMediaFormat_copy(AMediaFormat *to, AMediaFormat *from) {
61 if (!to || !from) {
62 return AMEDIA_ERROR_INVALID_PARAMETER;
63 }
64 to->mFormat->clear();
65 to->mFormat->extend(from->mFormat);
66 return AMEDIA_OK;
67 }
68
69
70 EXPORT
AMediaFormat_toString(AMediaFormat * mData)71 const char* AMediaFormat_toString(AMediaFormat *mData) {
72 sp<AMessage> f = mData->mFormat;
73 String8 ret;
74 int num = f->countEntries();
75 for (int i = 0; i < num; i++) {
76 if (i != 0) {
77 ret.append(", ");
78 }
79 AMessage::Type t;
80 const char *name = f->getEntryNameAt(i, &t);
81 ret.append(name);
82 ret.append(": ");
83 switch (t) {
84 case AMessage::kTypeInt32:
85 {
86 int32_t val;
87 f->findInt32(name, &val);
88 ret.appendFormat("int32(%" PRId32 ")", val);
89 break;
90 }
91 case AMessage::kTypeInt64:
92 {
93 int64_t val;
94 f->findInt64(name, &val);
95 ret.appendFormat("int64(%" PRId64 ")", val);
96 break;
97 }
98 case AMessage::kTypeSize:
99 {
100 size_t val;
101 f->findSize(name, &val);
102 ret.appendFormat("size_t(%zu)", val);
103 break;
104 }
105 case AMessage::kTypeFloat:
106 {
107 float val;
108 f->findFloat(name, &val);
109 ret.appendFormat("float(%f)", val);
110 break;
111 }
112 case AMessage::kTypeDouble:
113 {
114 double val;
115 f->findDouble(name, &val);
116 ret.appendFormat("double(%f)", val);
117 break;
118 }
119 case AMessage::kTypeRect:
120 {
121 int32_t left, top, right, bottom;
122 f->findRect(name, &left, &top, &right, &bottom);
123 ret.appendFormat("Rect(%" PRId32 ", %" PRId32 ", %" PRId32 ", %" PRId32 ")",
124 left, top, right, bottom);
125 break;
126 }
127 case AMessage::kTypeString:
128 {
129 AString val;
130 f->findString(name, &val);
131 ret.appendFormat("string(%s)", val.c_str());
132 break;
133 }
134 case AMessage::kTypeBuffer:
135 {
136 ret.appendFormat("data");
137 break;
138 }
139 default:
140 {
141 ret.appendFormat("unknown(%d)", t);
142 break;
143 }
144 }
145 }
146 ret.append("}");
147 mData->mDebug = ret;
148 return mData->mDebug.string();
149 }
150
151 EXPORT
AMediaFormat_getInt32(AMediaFormat * format,const char * name,int32_t * out)152 bool AMediaFormat_getInt32(AMediaFormat* format, const char *name, int32_t *out) {
153 return format->mFormat->findInt32(name, out);
154 }
155
156 EXPORT
AMediaFormat_getInt64(AMediaFormat * format,const char * name,int64_t * out)157 bool AMediaFormat_getInt64(AMediaFormat* format, const char *name, int64_t *out) {
158 return format->mFormat->findInt64(name, out);
159 }
160
161 EXPORT
AMediaFormat_getFloat(AMediaFormat * format,const char * name,float * out)162 bool AMediaFormat_getFloat(AMediaFormat* format, const char *name, float *out) {
163 return format->mFormat->findFloat(name, out);
164 }
165
166 EXPORT
AMediaFormat_getDouble(AMediaFormat * format,const char * name,double * out)167 bool AMediaFormat_getDouble(AMediaFormat* format, const char *name, double *out) {
168 return format->mFormat->findDouble(name, out);
169 }
170
171 EXPORT
AMediaFormat_getSize(AMediaFormat * format,const char * name,size_t * out)172 bool AMediaFormat_getSize(AMediaFormat* format, const char *name, size_t *out) {
173 return format->mFormat->findSize(name, out);
174 }
175
176 EXPORT
AMediaFormat_getRect(AMediaFormat * format,const char * name,int32_t * left,int32_t * top,int32_t * right,int32_t * bottom)177 bool AMediaFormat_getRect(AMediaFormat* format, const char *name,
178 int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) {
179 return format->mFormat->findRect(name, left, top, right, bottom);
180 }
181
182 EXPORT
AMediaFormat_getBuffer(AMediaFormat * format,const char * name,void ** data,size_t * outsize)183 bool AMediaFormat_getBuffer(AMediaFormat* format, const char *name, void** data, size_t *outsize) {
184 sp<ABuffer> buf;
185 if (format->mFormat->findBuffer(name, &buf)) {
186 *data = buf->data() + buf->offset();
187 *outsize = buf->size();
188 return true;
189 }
190 return false;
191 }
192
193 EXPORT
AMediaFormat_getString(AMediaFormat * mData,const char * name,const char ** out)194 bool AMediaFormat_getString(AMediaFormat* mData, const char *name, const char **out) {
195
196 for (size_t i = 0; i < mData->mStringCache.size(); i++) {
197 if (strcmp(mData->mStringCache.keyAt(i).string(), name) == 0) {
198 mData->mStringCache.removeItemsAt(i, 1);
199 break;
200 }
201 }
202
203 AString tmp;
204 if (mData->mFormat->findString(name, &tmp)) {
205 String8 ret(tmp.c_str());
206 mData->mStringCache.add(String8(name), ret);
207 *out = ret.string();
208 return true;
209 }
210 return false;
211 }
212
213 EXPORT
AMediaFormat_setInt32(AMediaFormat * format,const char * name,int32_t value)214 void AMediaFormat_setInt32(AMediaFormat* format, const char *name, int32_t value) {
215 format->mFormat->setInt32(name, value);
216 }
217
218 EXPORT
AMediaFormat_setInt64(AMediaFormat * format,const char * name,int64_t value)219 void AMediaFormat_setInt64(AMediaFormat* format, const char *name, int64_t value) {
220 format->mFormat->setInt64(name, value);
221 }
222
223 EXPORT
AMediaFormat_setFloat(AMediaFormat * format,const char * name,float value)224 void AMediaFormat_setFloat(AMediaFormat* format, const char* name, float value) {
225 format->mFormat->setFloat(name, value);
226 }
227
228 EXPORT
AMediaFormat_setDouble(AMediaFormat * format,const char * name,double value)229 void AMediaFormat_setDouble(AMediaFormat* format, const char* name, double value) {
230 format->mFormat->setDouble(name, value);
231 }
232
233 EXPORT
AMediaFormat_setSize(AMediaFormat * format,const char * name,size_t value)234 void AMediaFormat_setSize(AMediaFormat* format, const char* name, size_t value) {
235 format->mFormat->setSize(name, value);
236 }
237
238 EXPORT
AMediaFormat_setRect(AMediaFormat * format,const char * name,int32_t left,int32_t top,int32_t right,int32_t bottom)239 void AMediaFormat_setRect(AMediaFormat* format, const char *name,
240 int32_t left, int32_t top, int32_t right, int32_t bottom) {
241 format->mFormat->setRect(name, left, top, right, bottom);
242 }
243
244 EXPORT
AMediaFormat_setString(AMediaFormat * format,const char * name,const char * value)245 void AMediaFormat_setString(AMediaFormat* format, const char* name, const char* value) {
246 // AMessage::setString() makes a copy of the string
247 format->mFormat->setString(name, value, strlen(value));
248 }
249
250 EXPORT
AMediaFormat_setBuffer(AMediaFormat * format,const char * name,const void * data,size_t size)251 void AMediaFormat_setBuffer(AMediaFormat* format, const char* name, const void* data, size_t size) {
252 // the ABuffer(void*, size_t) constructor doesn't take ownership of the data, so create
253 // a new buffer and copy the data into it
254 sp<ABuffer> buf = new ABuffer(size);
255 memcpy(buf->data(), data, size);
256 buf->setRange(0, size);
257 // AMessage::setBuffer() increases the refcount of the buffer
258 format->mFormat->setBuffer(name, buf);
259 }
260
261
262 EXPORT const char* AMEDIAFORMAT_KEY_AAC_DRC_ATTENUATION_FACTOR = "aac-drc-cut-level";
263 EXPORT const char* AMEDIAFORMAT_KEY_AAC_DRC_BOOST_FACTOR = "aac-drc-boost-level";
264 EXPORT const char* AMEDIAFORMAT_KEY_AAC_DRC_HEAVY_COMPRESSION = "aac-drc-heavy-compression";
265 EXPORT const char* AMEDIAFORMAT_KEY_AAC_DRC_TARGET_REFERENCE_LEVEL = "aac-target-ref-level";
266 EXPORT const char* AMEDIAFORMAT_KEY_AAC_ENCODED_TARGET_LEVEL = "aac-encoded-target-level";
267 EXPORT const char* AMEDIAFORMAT_KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT = "aac-max-output-channel_count";
268 EXPORT const char* AMEDIAFORMAT_KEY_AAC_PROFILE = "aac-profile";
269 EXPORT const char* AMEDIAFORMAT_KEY_AAC_SBR_MODE = "aac-sbr-mode";
270 EXPORT const char* AMEDIAFORMAT_KEY_ALBUM = "album";
271 EXPORT const char* AMEDIAFORMAT_KEY_ALBUMART = "albumart";
272 EXPORT const char* AMEDIAFORMAT_KEY_ALBUMARTIST = "albumartist";
273 EXPORT const char* AMEDIAFORMAT_KEY_ARTIST = "artist";
274 EXPORT const char* AMEDIAFORMAT_KEY_AUDIO_PRESENTATION_INFO = "audio-presentation-info";
275 EXPORT const char* AMEDIAFORMAT_KEY_AUDIO_PRESENTATION_PRESENTATION_ID =
276 "audio-presentation-presentation-id";
277 EXPORT const char* AMEDIAFORMAT_KEY_AUDIO_PRESENTATION_PROGRAM_ID = "audio-presentation-program-id";
278 EXPORT const char* AMEDIAFORMAT_KEY_AUDIO_SESSION_ID = "audio-session-id";
279 EXPORT const char* AMEDIAFORMAT_KEY_AUTHOR = "author";
280 EXPORT const char* AMEDIAFORMAT_KEY_BITRATE_MODE = "bitrate-mode";
281 EXPORT const char* AMEDIAFORMAT_KEY_BIT_RATE = "bitrate";
282 EXPORT const char* AMEDIAFORMAT_KEY_BITS_PER_SAMPLE = "bits-per-sample";
283 EXPORT const char* AMEDIAFORMAT_KEY_CAPTURE_RATE = "capture-rate";
284 EXPORT const char* AMEDIAFORMAT_KEY_CDTRACKNUMBER = "cdtracknum";
285 EXPORT const char* AMEDIAFORMAT_KEY_CHANNEL_COUNT = "channel-count";
286 EXPORT const char* AMEDIAFORMAT_KEY_CHANNEL_MASK = "channel-mask";
287 EXPORT const char* AMEDIAFORMAT_KEY_COLOR_FORMAT = "color-format";
288 EXPORT const char* AMEDIAFORMAT_KEY_COLOR_RANGE = "color-range";
289 EXPORT const char* AMEDIAFORMAT_KEY_COLOR_STANDARD = "color-standard";
290 EXPORT const char* AMEDIAFORMAT_KEY_COLOR_TRANSFER = "color-transfer";
291 EXPORT const char* AMEDIAFORMAT_KEY_COMPILATION = "compilation";
292 EXPORT const char* AMEDIAFORMAT_KEY_COMPLEXITY = "complexity";
293 EXPORT const char* AMEDIAFORMAT_KEY_COMPOSER = "composer";
294 EXPORT const char* AMEDIAFORMAT_KEY_CREATE_INPUT_SURFACE_SUSPENDED = "create-input-buffers-suspended";
295 EXPORT const char* AMEDIAFORMAT_KEY_CRYPTO_DEFAULT_IV_SIZE = "crypto-default-iv-size";
296 EXPORT const char* AMEDIAFORMAT_KEY_CRYPTO_ENCRYPTED_BYTE_BLOCK = "crypto-encrypted-byte-block";
297 EXPORT const char* AMEDIAFORMAT_KEY_CRYPTO_ENCRYPTED_SIZES = "crypto-encrypted-sizes";
298 EXPORT const char* AMEDIAFORMAT_KEY_CRYPTO_IV = "crypto-iv";
299 EXPORT const char* AMEDIAFORMAT_KEY_CRYPTO_KEY = "crypto-key";
300 EXPORT const char* AMEDIAFORMAT_KEY_CRYPTO_MODE = "crypto-mode";
301 EXPORT const char* AMEDIAFORMAT_KEY_CRYPTO_PLAIN_SIZES = "crypto-plain-sizes";
302 EXPORT const char* AMEDIAFORMAT_KEY_CRYPTO_SKIP_BYTE_BLOCK = "crypto-skip-byte-block";
303 EXPORT const char* AMEDIAFORMAT_KEY_CSD = "csd";
304 EXPORT const char* AMEDIAFORMAT_KEY_CSD_0 = "csd-0";
305 EXPORT const char* AMEDIAFORMAT_KEY_CSD_1 = "csd-1";
306 EXPORT const char* AMEDIAFORMAT_KEY_CSD_2 = "csd-2";
307 EXPORT const char* AMEDIAFORMAT_KEY_CSD_AVC = "csd-avc";
308 EXPORT const char* AMEDIAFORMAT_KEY_CSD_HEVC = "csd-hevc";
309 EXPORT const char* AMEDIAFORMAT_KEY_D263 = "d263";
310 EXPORT const char* AMEDIAFORMAT_KEY_DATE = "date";
311 EXPORT const char* AMEDIAFORMAT_KEY_DISCNUMBER = "discnum";
312 EXPORT const char* AMEDIAFORMAT_KEY_DISPLAY_CROP = "crop";
313 EXPORT const char* AMEDIAFORMAT_KEY_DISPLAY_HEIGHT = "display-height";
314 EXPORT const char* AMEDIAFORMAT_KEY_DISPLAY_WIDTH = "display-width";
315 EXPORT const char* AMEDIAFORMAT_KEY_DURATION = "durationUs";
316 EXPORT const char* AMEDIAFORMAT_KEY_ENCODER_DELAY = "encoder-delay";
317 EXPORT const char* AMEDIAFORMAT_KEY_ENCODER_PADDING = "encoder-padding";
318 EXPORT const char* AMEDIAFORMAT_KEY_ESDS = "esds";
319 EXPORT const char* AMEDIAFORMAT_KEY_EXIF_OFFSET = "exif-offset";
320 EXPORT const char* AMEDIAFORMAT_KEY_EXIF_SIZE = "exif-size";
321 EXPORT const char* AMEDIAFORMAT_KEY_FLAC_COMPRESSION_LEVEL = "flac-compression-level";
322 EXPORT const char* AMEDIAFORMAT_KEY_FRAME_COUNT = "frame-count";
323 EXPORT const char* AMEDIAFORMAT_KEY_FRAME_RATE = "frame-rate";
324 EXPORT const char* AMEDIAFORMAT_KEY_GENRE = "genre";
325 EXPORT const char* AMEDIAFORMAT_KEY_GRID_COLUMNS = "grid-cols";
326 EXPORT const char* AMEDIAFORMAT_KEY_GRID_ROWS = "grid-rows";
327 EXPORT const char* AMEDIAFORMAT_KEY_HAPTIC_CHANNEL_COUNT = "haptic-channel-count";
328 EXPORT const char* AMEDIAFORMAT_KEY_HDR_STATIC_INFO = "hdr-static-info";
329 EXPORT const char* AMEDIAFORMAT_KEY_HDR10_PLUS_INFO = "hdr10-plus-info";
330 EXPORT const char* AMEDIAFORMAT_KEY_HEIGHT = "height";
331 EXPORT const char* AMEDIAFORMAT_KEY_ICC_PROFILE = "icc-profile";
332 EXPORT const char* AMEDIAFORMAT_KEY_INTRA_REFRESH_PERIOD = "intra-refresh-period";
333 EXPORT const char* AMEDIAFORMAT_KEY_IS_ADTS = "is-adts";
334 EXPORT const char* AMEDIAFORMAT_KEY_IS_AUTOSELECT = "is-autoselect";
335 EXPORT const char* AMEDIAFORMAT_KEY_IS_DEFAULT = "is-default";
336 EXPORT const char* AMEDIAFORMAT_KEY_IS_FORCED_SUBTITLE = "is-forced-subtitle";
337 EXPORT const char* AMEDIAFORMAT_KEY_IS_SYNC_FRAME = "is-sync-frame";
338 EXPORT const char* AMEDIAFORMAT_KEY_I_FRAME_INTERVAL = "i-frame-interval";
339 EXPORT const char* AMEDIAFORMAT_KEY_LANGUAGE = "language";
340 EXPORT const char* AMEDIAFORMAT_KEY_LATENCY = "latency";
341 EXPORT const char* AMEDIAFORMAT_KEY_LEVEL = "level";
342 EXPORT const char* AMEDIAFORMAT_KEY_LOCATION = "location";
343 EXPORT const char* AMEDIAFORMAT_KEY_LOOP = "loop";
344 EXPORT const char* AMEDIAFORMAT_KEY_LOW_LATENCY = "low-latency";
345 EXPORT const char* AMEDIAFORMAT_KEY_LYRICIST = "lyricist";
346 EXPORT const char* AMEDIAFORMAT_KEY_MANUFACTURER = "manufacturer";
347 EXPORT const char* AMEDIAFORMAT_KEY_MAX_BIT_RATE = "max-bitrate";
348 EXPORT const char* AMEDIAFORMAT_KEY_MAX_FPS_TO_ENCODER = "max-fps-to-encoder";
349 EXPORT const char* AMEDIAFORMAT_KEY_MAX_HEIGHT = "max-height";
350 EXPORT const char* AMEDIAFORMAT_KEY_MAX_INPUT_SIZE = "max-input-size";
351 EXPORT const char* AMEDIAFORMAT_KEY_MAX_PTS_GAP_TO_ENCODER = "max-pts-gap-to-encoder";
352 EXPORT const char* AMEDIAFORMAT_KEY_MAX_WIDTH = "max-width";
353 EXPORT const char* AMEDIAFORMAT_KEY_MIME = "mime";
354 EXPORT const char* AMEDIAFORMAT_KEY_MPEG_USER_DATA = "mpeg-user-data";
355 EXPORT const char* AMEDIAFORMAT_KEY_MPEG2_STREAM_HEADER = "mpeg2-stream-header";
356 EXPORT const char* AMEDIAFORMAT_KEY_OPERATING_RATE = "operating-rate";
357 EXPORT const char* AMEDIAFORMAT_KEY_PCM_ENCODING = "pcm-encoding";
358 EXPORT const char* AMEDIAFORMAT_KEY_PRIORITY = "priority";
359 EXPORT const char* AMEDIAFORMAT_KEY_PROFILE = "profile";
360 EXPORT const char* AMEDIAFORMAT_KEY_PCM_BIG_ENDIAN = "pcm-big-endian";
361 EXPORT const char* AMEDIAFORMAT_KEY_PSSH = "pssh";
362 EXPORT const char* AMEDIAFORMAT_KEY_PUSH_BLANK_BUFFERS_ON_STOP = "push-blank-buffers-on-shutdown";
363 EXPORT const char* AMEDIAFORMAT_KEY_REPEAT_PREVIOUS_FRAME_AFTER = "repeat-previous-frame-after";
364 EXPORT const char* AMEDIAFORMAT_KEY_ROTATION = "rotation-degrees";
365 EXPORT const char* AMEDIAFORMAT_KEY_SAMPLE_RATE = "sample-rate";
366 EXPORT const char* AMEDIAFORMAT_KEY_SAR_HEIGHT = "sar-height";
367 EXPORT const char* AMEDIAFORMAT_KEY_SAR_WIDTH = "sar-width";
368 EXPORT const char* AMEDIAFORMAT_KEY_SEI = "sei";
369 EXPORT const char* AMEDIAFORMAT_KEY_SLICE_HEIGHT = "slice-height";
370 EXPORT const char* AMEDIAFORMAT_KEY_STRIDE = "stride";
371 EXPORT const char* AMEDIAFORMAT_KEY_TARGET_TIME = "target-time";
372 EXPORT const char* AMEDIAFORMAT_KEY_TEMPORAL_LAYER_COUNT = "temporal-layer-count";
373 EXPORT const char* AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID = "temporal-layer-id";
374 EXPORT const char* AMEDIAFORMAT_KEY_TEMPORAL_LAYERING = "ts-schema";
375 EXPORT const char* AMEDIAFORMAT_KEY_TEXT_FORMAT_DATA = "text-format-data";
376 EXPORT const char* AMEDIAFORMAT_KEY_THUMBNAIL_CSD_HEVC = "thumbnail-csd-hevc";
377 EXPORT const char* AMEDIAFORMAT_KEY_THUMBNAIL_HEIGHT = "thumbnail-height";
378 EXPORT const char* AMEDIAFORMAT_KEY_THUMBNAIL_TIME = "thumbnail-time";
379 EXPORT const char* AMEDIAFORMAT_KEY_THUMBNAIL_WIDTH = "thumbnail-width";
380 EXPORT const char* AMEDIAFORMAT_KEY_TILE_HEIGHT = "tile-height";
381 EXPORT const char* AMEDIAFORMAT_KEY_TILE_WIDTH = "tile-width";
382 EXPORT const char* AMEDIAFORMAT_KEY_TIME_US = "timeUs";
383 EXPORT const char* AMEDIAFORMAT_KEY_TITLE = "title";
384 EXPORT const char* AMEDIAFORMAT_KEY_TRACK_ID = "track-id";
385 EXPORT const char* AMEDIAFORMAT_KEY_TRACK_INDEX = "track-index";
386 EXPORT const char* AMEDIAFORMAT_KEY_VALID_SAMPLES = "valid-samples";
387 EXPORT const char* AMEDIAFORMAT_KEY_WIDTH = "width";
388 EXPORT const char* AMEDIAFORMAT_KEY_YEAR = "year";
389
390 } // extern "C"
391
392
393