Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 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 | /* |
| 18 | * This file defines an NDK API. |
| 19 | * Do not remove methods. |
| 20 | * Do not change method signatures. |
| 21 | * Do not change the value of constants. |
| 22 | * Do not change the size of any of the classes defined in here. |
| 23 | * Do not reference types that are not part of the NDK. |
| 24 | * Do not #include files that aren't part of the NDK. |
| 25 | */ |
| 26 | |
| 27 | #ifndef _NDK_MEDIA_CODEC_H |
| 28 | #define _NDK_MEDIA_CODEC_H |
| 29 | |
| 30 | #include <android/native_window.h> |
| 31 | |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 32 | #include "NdkMediaCrypto.h" |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 33 | #include "NdkMediaError.h" |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 34 | #include "NdkMediaFormat.h" |
| 35 | |
| 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | |
| 41 | struct AMediaCodec; |
| 42 | typedef struct AMediaCodec AMediaCodec; |
| 43 | |
| 44 | struct AMediaCodecBufferInfo { |
| 45 | int32_t offset; |
| 46 | int32_t size; |
| 47 | int64_t presentationTimeUs; |
| 48 | uint32_t flags; |
| 49 | }; |
| 50 | typedef struct AMediaCodecBufferInfo AMediaCodecBufferInfo; |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 51 | typedef struct AMediaCodecCryptoInfo AMediaCodecCryptoInfo; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 52 | |
| 53 | enum { |
| 54 | AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM = 4, |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 55 | AMEDIACODEC_CONFIGURE_FLAG_ENCODE = 1, |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 56 | AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED = -3, |
| 57 | AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED = -2, |
| 58 | AMEDIACODEC_INFO_TRY_AGAIN_LATER = -1 |
| 59 | }; |
| 60 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 61 | /** |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 62 | * Create codec by name. Use this if you know the exact codec you want to use. |
| 63 | * When configuring, you will need to specify whether to use the codec as an |
| 64 | * encoder or decoder. |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 65 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 66 | AMediaCodec* AMediaCodec_createCodecByName(const char *name); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Create codec by mime type. Most applications will use this, specifying a |
| 70 | * mime type obtained from media extractor. |
| 71 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 72 | AMediaCodec* AMediaCodec_createDecoderByType(const char *mime_type); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Create encoder by name. |
| 76 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 77 | AMediaCodec* AMediaCodec_createEncoderByType(const char *mime_type); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * delete the codec and free its resources |
| 81 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 82 | media_status_t AMediaCodec_delete(AMediaCodec*); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Configure the codec. For decoding you would typically get the format from an extractor. |
| 86 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 87 | media_status_t AMediaCodec_configure( |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 88 | AMediaCodec*, |
| 89 | const AMediaFormat* format, |
| 90 | ANativeWindow* surface, |
| 91 | AMediaCrypto *crypto, |
| 92 | uint32_t flags); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Start the codec. A codec must be configured before it can be started, and must be started |
| 96 | * before buffers can be sent to it. |
| 97 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 98 | media_status_t AMediaCodec_start(AMediaCodec*); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Stop the codec. |
| 102 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 103 | media_status_t AMediaCodec_stop(AMediaCodec*); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Flush the codec's input and output. All indices previously returned from calls to |
| 107 | * AMediaCodec_dequeueInputBuffer and AMediaCodec_dequeueOutputBuffer become invalid. |
| 108 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 109 | media_status_t AMediaCodec_flush(AMediaCodec*); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Get an input buffer. The specified buffer index must have been previously obtained from |
| 113 | * dequeueInputBuffer, and not yet queued. |
| 114 | */ |
| 115 | uint8_t* AMediaCodec_getInputBuffer(AMediaCodec*, size_t idx, size_t *out_size); |
| 116 | |
| 117 | /** |
| 118 | * Get an output buffer. The specified buffer index must have been previously obtained from |
| 119 | * dequeueOutputBuffer, and not yet queued. |
| 120 | */ |
| 121 | uint8_t* AMediaCodec_getOutputBuffer(AMediaCodec*, size_t idx, size_t *out_size); |
| 122 | |
| 123 | /** |
| 124 | * Get the index of the next available input buffer. An app will typically use this with |
| 125 | * getInputBuffer() to get a pointer to the buffer, then copy the data to be encoded or decoded |
| 126 | * into the buffer before passing it to the codec. |
| 127 | */ |
| 128 | ssize_t AMediaCodec_dequeueInputBuffer(AMediaCodec*, int64_t timeoutUs); |
| 129 | |
| 130 | /** |
| 131 | * Send the specified buffer to the codec for processing. |
| 132 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 133 | media_status_t AMediaCodec_queueInputBuffer(AMediaCodec*, |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 134 | size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags); |
| 135 | |
| 136 | /** |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 137 | * Send the specified buffer to the codec for processing. |
| 138 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 139 | media_status_t AMediaCodec_queueSecureInputBuffer(AMediaCodec*, |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 140 | size_t idx, off_t offset, AMediaCodecCryptoInfo*, uint64_t time, uint32_t flags); |
| 141 | |
| 142 | /** |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 143 | * Get the index of the next available buffer of processed data. |
| 144 | */ |
| 145 | ssize_t AMediaCodec_dequeueOutputBuffer(AMediaCodec*, AMediaCodecBufferInfo *info, int64_t timeoutUs); |
| 146 | AMediaFormat* AMediaCodec_getOutputFormat(AMediaCodec*); |
| 147 | |
| 148 | /** |
| 149 | * Release and optionally render the specified buffer. |
| 150 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 151 | media_status_t AMediaCodec_releaseOutputBuffer(AMediaCodec*, size_t idx, bool render); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 152 | |
| 153 | |
Marco Nelissen | cdb42cd | 2014-05-08 14:46:05 -0700 | [diff] [blame] | 154 | typedef void (*OnCodecEvent)(AMediaCodec *codec, void *userdata); |
| 155 | |
| 156 | /** |
| 157 | * Set a callback to be called when a new buffer is available, or there was a format |
| 158 | * or buffer change. |
| 159 | * Note that you cannot perform any operations on the mediacodec from within the callback. |
| 160 | * If you need to perform mediacodec operations, you must do so on a different thread. |
| 161 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 162 | media_status_t AMediaCodec_setNotificationCallback( |
| 163 | AMediaCodec*, OnCodecEvent callback, void *userdata); |
Marco Nelissen | cdb42cd | 2014-05-08 14:46:05 -0700 | [diff] [blame] | 164 | |
| 165 | |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 166 | enum { |
| 167 | AMEDIACODECRYPTOINFO_MODE_CLEAR = 0, |
| 168 | AMEDIACODECRYPTOINFO_MODE_AES_CTR = 1 |
| 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * create an AMediaCodecCryptoInfo from scratch. Use this if you need to use custom |
| 173 | * crypto info, rather than one obtained from AMediaExtractor. |
| 174 | */ |
| 175 | AMediaCodecCryptoInfo *AMediaCodecCryptoInfo_new( |
| 176 | int numsubsamples, |
| 177 | uint8_t key[16], |
| 178 | uint8_t iv[16], |
| 179 | uint32_t mode, |
| 180 | size_t *clearbytes, |
| 181 | size_t *encryptedbytes); |
| 182 | |
| 183 | /** |
Marco Nelissen | 829e097 | 2014-05-13 16:22:19 -0700 | [diff] [blame] | 184 | * delete an AMediaCodecCryptoInfo created previously with AMediaCodecCryptoInfo_new, or |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 185 | * obtained from AMediaExtractor |
| 186 | */ |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 187 | media_status_t AMediaCodecCryptoInfo_delete(AMediaCodecCryptoInfo*); |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 188 | |
| 189 | size_t AMediaCodecCryptoInfo_getNumSubSamples(AMediaCodecCryptoInfo*); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 190 | media_status_t AMediaCodecCryptoInfo_getKey(AMediaCodecCryptoInfo*, uint8_t *dst); |
| 191 | media_status_t AMediaCodecCryptoInfo_getIV(AMediaCodecCryptoInfo*, uint8_t *dst); |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 192 | uint32_t AMediaCodecCryptoInfo_getMode(AMediaCodecCryptoInfo*); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame^] | 193 | media_status_t AMediaCodecCryptoInfo_getClearBytes(AMediaCodecCryptoInfo*, size_t *dst); |
| 194 | media_status_t AMediaCodecCryptoInfo_getEncryptedBytes(AMediaCodecCryptoInfo*, size_t *dst); |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 195 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 196 | #ifdef __cplusplus |
| 197 | } // extern "C" |
| 198 | #endif |
| 199 | |
| 200 | #endif //_NDK_MEDIA_CODEC_H |