blob: 2f000d73e13545cc59be0e8224febd599907fa8e [file] [log] [blame]
Marco Nelissen0c3be872014-05-01 10:14:44 -07001/*
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 Nelissen050eb322014-05-09 15:10:23 -070032#include "NdkMediaCrypto.h"
Marco Nelissene419d7c2014-05-15 14:17:25 -070033#include "NdkMediaError.h"
Marco Nelissen0c3be872014-05-01 10:14:44 -070034#include "NdkMediaFormat.h"
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40
41struct AMediaCodec;
42typedef struct AMediaCodec AMediaCodec;
43
44struct AMediaCodecBufferInfo {
45 int32_t offset;
46 int32_t size;
47 int64_t presentationTimeUs;
48 uint32_t flags;
49};
50typedef struct AMediaCodecBufferInfo AMediaCodecBufferInfo;
Marco Nelissen050eb322014-05-09 15:10:23 -070051typedef struct AMediaCodecCryptoInfo AMediaCodecCryptoInfo;
Marco Nelissen0c3be872014-05-01 10:14:44 -070052
53enum {
54 AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM = 4,
Marco Nelissen86aa02c2014-05-07 16:03:54 -070055 AMEDIACODEC_CONFIGURE_FLAG_ENCODE = 1,
Marco Nelissen0c3be872014-05-01 10:14:44 -070056 AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED = -3,
57 AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED = -2,
58 AMEDIACODEC_INFO_TRY_AGAIN_LATER = -1
59};
60
Marco Nelissen0c3be872014-05-01 10:14:44 -070061/**
Marco Nelissen86aa02c2014-05-07 16:03:54 -070062 * 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 Nelissen0c3be872014-05-01 10:14:44 -070065 */
Marco Nelissen86aa02c2014-05-07 16:03:54 -070066AMediaCodec* AMediaCodec_createCodecByName(const char *name);
Marco Nelissen0c3be872014-05-01 10:14:44 -070067
68/**
69 * Create codec by mime type. Most applications will use this, specifying a
70 * mime type obtained from media extractor.
71 */
Marco Nelissen86aa02c2014-05-07 16:03:54 -070072AMediaCodec* AMediaCodec_createDecoderByType(const char *mime_type);
Marco Nelissen0c3be872014-05-01 10:14:44 -070073
74/**
75 * Create encoder by name.
76 */
Marco Nelissen86aa02c2014-05-07 16:03:54 -070077AMediaCodec* AMediaCodec_createEncoderByType(const char *mime_type);
Marco Nelissen0c3be872014-05-01 10:14:44 -070078
79/**
80 * delete the codec and free its resources
81 */
Marco Nelissene419d7c2014-05-15 14:17:25 -070082media_status_t AMediaCodec_delete(AMediaCodec*);
Marco Nelissen0c3be872014-05-01 10:14:44 -070083
84/**
85 * Configure the codec. For decoding you would typically get the format from an extractor.
86 */
Marco Nelissene419d7c2014-05-15 14:17:25 -070087media_status_t AMediaCodec_configure(
Marco Nelissen050eb322014-05-09 15:10:23 -070088 AMediaCodec*,
89 const AMediaFormat* format,
90 ANativeWindow* surface,
91 AMediaCrypto *crypto,
92 uint32_t flags);
Marco Nelissen0c3be872014-05-01 10:14:44 -070093
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 Nelissene419d7c2014-05-15 14:17:25 -070098media_status_t AMediaCodec_start(AMediaCodec*);
Marco Nelissen0c3be872014-05-01 10:14:44 -070099
100/**
101 * Stop the codec.
102 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700103media_status_t AMediaCodec_stop(AMediaCodec*);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700104
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 Nelissene419d7c2014-05-15 14:17:25 -0700109media_status_t AMediaCodec_flush(AMediaCodec*);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700110
111/**
112 * Get an input buffer. The specified buffer index must have been previously obtained from
113 * dequeueInputBuffer, and not yet queued.
114 */
115uint8_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 */
121uint8_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 */
128ssize_t AMediaCodec_dequeueInputBuffer(AMediaCodec*, int64_t timeoutUs);
129
130/**
131 * Send the specified buffer to the codec for processing.
132 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700133media_status_t AMediaCodec_queueInputBuffer(AMediaCodec*,
Marco Nelissen0c3be872014-05-01 10:14:44 -0700134 size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags);
135
136/**
Marco Nelissen050eb322014-05-09 15:10:23 -0700137 * Send the specified buffer to the codec for processing.
138 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700139media_status_t AMediaCodec_queueSecureInputBuffer(AMediaCodec*,
Marco Nelissen050eb322014-05-09 15:10:23 -0700140 size_t idx, off_t offset, AMediaCodecCryptoInfo*, uint64_t time, uint32_t flags);
141
142/**
Marco Nelissen0c3be872014-05-01 10:14:44 -0700143 * Get the index of the next available buffer of processed data.
144 */
145ssize_t AMediaCodec_dequeueOutputBuffer(AMediaCodec*, AMediaCodecBufferInfo *info, int64_t timeoutUs);
146AMediaFormat* AMediaCodec_getOutputFormat(AMediaCodec*);
147
148/**
Marco Nelissen79e2b622014-05-16 08:07:28 -0700149 * If you are done with a buffer, use this call to return the buffer to
150 * the codec. If you previously specified a surface when configuring this
151 * video decoder you can optionally render the buffer.
Marco Nelissen0c3be872014-05-01 10:14:44 -0700152 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700153media_status_t AMediaCodec_releaseOutputBuffer(AMediaCodec*, size_t idx, bool render);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700154
Marco Nelissen79e2b622014-05-16 08:07:28 -0700155/**
156 * If you are done with a buffer, use this call to update its surface timestamp
157 * and return it to the codec to render it on the output surface. If you
158 * have not specified an output surface when configuring this video codec,
159 * this call will simply return the buffer to the codec.
160 *
161 * For more details, see the Java documentation for MediaCodec.releaseOutputBuffer.
162 */
163media_status_t AMediaCodec_releaseOutputBufferAtTime(
164 AMediaCodec *mData, size_t idx, int64_t timestampNs);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700165
Marco Nelissencdb42cd2014-05-08 14:46:05 -0700166typedef void (*OnCodecEvent)(AMediaCodec *codec, void *userdata);
167
168/**
169 * Set a callback to be called when a new buffer is available, or there was a format
170 * or buffer change.
171 * Note that you cannot perform any operations on the mediacodec from within the callback.
172 * If you need to perform mediacodec operations, you must do so on a different thread.
173 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700174media_status_t AMediaCodec_setNotificationCallback(
175 AMediaCodec*, OnCodecEvent callback, void *userdata);
Marco Nelissencdb42cd2014-05-08 14:46:05 -0700176
177
Marco Nelissen79e2b622014-05-16 08:07:28 -0700178typedef enum {
Marco Nelissen050eb322014-05-09 15:10:23 -0700179 AMEDIACODECRYPTOINFO_MODE_CLEAR = 0,
180 AMEDIACODECRYPTOINFO_MODE_AES_CTR = 1
Marco Nelissen79e2b622014-05-16 08:07:28 -0700181} cryptoinfo_mode_t;
Marco Nelissen050eb322014-05-09 15:10:23 -0700182
183/**
Marco Nelissen79e2b622014-05-16 08:07:28 -0700184 * Create an AMediaCodecCryptoInfo from scratch. Use this if you need to use custom
Marco Nelissen050eb322014-05-09 15:10:23 -0700185 * crypto info, rather than one obtained from AMediaExtractor.
Marco Nelissen79e2b622014-05-16 08:07:28 -0700186 *
187 * AMediaCodecCryptoInfo describes the structure of an (at least
188 * partially) encrypted input sample.
189 * A buffer's data is considered to be partitioned into "subsamples",
190 * each subsample starts with a (potentially empty) run of plain,
191 * unencrypted bytes followed by a (also potentially empty) run of
192 * encrypted bytes.
193 * numBytesOfClearData can be null to indicate that all data is encrypted.
194 * This information encapsulates per-sample metadata as outlined in
195 * ISO/IEC FDIS 23001-7:2011 "Common encryption in ISO base media file format files".
Marco Nelissen050eb322014-05-09 15:10:23 -0700196 */
197AMediaCodecCryptoInfo *AMediaCodecCryptoInfo_new(
198 int numsubsamples,
199 uint8_t key[16],
200 uint8_t iv[16],
Marco Nelissen79e2b622014-05-16 08:07:28 -0700201 cryptoinfo_mode_t mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700202 size_t *clearbytes,
203 size_t *encryptedbytes);
204
205/**
Marco Nelissen829e0972014-05-13 16:22:19 -0700206 * delete an AMediaCodecCryptoInfo created previously with AMediaCodecCryptoInfo_new, or
Marco Nelissen050eb322014-05-09 15:10:23 -0700207 * obtained from AMediaExtractor
208 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700209media_status_t AMediaCodecCryptoInfo_delete(AMediaCodecCryptoInfo*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700210
Marco Nelissen79e2b622014-05-16 08:07:28 -0700211/**
212 * The number of subsamples that make up the buffer's contents.
213 */
Marco Nelissen050eb322014-05-09 15:10:23 -0700214size_t AMediaCodecCryptoInfo_getNumSubSamples(AMediaCodecCryptoInfo*);
Marco Nelissen79e2b622014-05-16 08:07:28 -0700215
216/**
217 * A 16-byte opaque key
218 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700219media_status_t AMediaCodecCryptoInfo_getKey(AMediaCodecCryptoInfo*, uint8_t *dst);
Marco Nelissen79e2b622014-05-16 08:07:28 -0700220
221/**
222 * A 16-byte initialization vector
223 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700224media_status_t AMediaCodecCryptoInfo_getIV(AMediaCodecCryptoInfo*, uint8_t *dst);
Marco Nelissen79e2b622014-05-16 08:07:28 -0700225
226/**
227 * The type of encryption that has been applied,
228 * one of AMEDIACODECRYPTOINFO_MODE_CLEAR or AMEDIACODECRYPTOINFO_MODE_AES_CTR.
229 */
230cryptoinfo_mode_t AMediaCodecCryptoInfo_getMode(AMediaCodecCryptoInfo*);
231
232/**
233 * The number of leading unencrypted bytes in each subsample.
234 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700235media_status_t AMediaCodecCryptoInfo_getClearBytes(AMediaCodecCryptoInfo*, size_t *dst);
Marco Nelissen79e2b622014-05-16 08:07:28 -0700236
237/**
238 * The number of trailing encrypted bytes in each subsample.
239 */
Marco Nelissene419d7c2014-05-15 14:17:25 -0700240media_status_t AMediaCodecCryptoInfo_getEncryptedBytes(AMediaCodecCryptoInfo*, size_t *dst);
Marco Nelissen050eb322014-05-09 15:10:23 -0700241
Marco Nelissen0c3be872014-05-01 10:14:44 -0700242#ifdef __cplusplus
243} // extern "C"
244#endif
245
246#endif //_NDK_MEDIA_CODEC_H