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