blob: 28d121c0403a752fdc4b0197582ed8245b8b831e [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 Nelissen0c3be872014-05-01 10:14:44 -070033#include "NdkMediaFormat.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39
40struct AMediaCodec;
41typedef struct AMediaCodec AMediaCodec;
42
43struct AMediaCodecBufferInfo {
44 int32_t offset;
45 int32_t size;
46 int64_t presentationTimeUs;
47 uint32_t flags;
48};
49typedef struct AMediaCodecBufferInfo AMediaCodecBufferInfo;
Marco Nelissen050eb322014-05-09 15:10:23 -070050typedef struct AMediaCodecCryptoInfo AMediaCodecCryptoInfo;
Marco Nelissen0c3be872014-05-01 10:14:44 -070051
52enum {
53 AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM = 4,
Marco Nelissen86aa02c2014-05-07 16:03:54 -070054 AMEDIACODEC_CONFIGURE_FLAG_ENCODE = 1,
Marco Nelissen0c3be872014-05-01 10:14:44 -070055 AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED = -3,
56 AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED = -2,
57 AMEDIACODEC_INFO_TRY_AGAIN_LATER = -1
58};
59
Marco Nelissen0c3be872014-05-01 10:14:44 -070060/**
Marco Nelissen86aa02c2014-05-07 16:03:54 -070061 * Create codec by name. Use this if you know the exact codec you want to use.
62 * When configuring, you will need to specify whether to use the codec as an
63 * encoder or decoder.
Marco Nelissen0c3be872014-05-01 10:14:44 -070064 */
Marco Nelissen86aa02c2014-05-07 16:03:54 -070065AMediaCodec* AMediaCodec_createCodecByName(const char *name);
Marco Nelissen0c3be872014-05-01 10:14:44 -070066
67/**
68 * Create codec by mime type. Most applications will use this, specifying a
69 * mime type obtained from media extractor.
70 */
Marco Nelissen86aa02c2014-05-07 16:03:54 -070071AMediaCodec* AMediaCodec_createDecoderByType(const char *mime_type);
Marco Nelissen0c3be872014-05-01 10:14:44 -070072
73/**
74 * Create encoder by name.
75 */
Marco Nelissen86aa02c2014-05-07 16:03:54 -070076AMediaCodec* AMediaCodec_createEncoderByType(const char *mime_type);
Marco Nelissen0c3be872014-05-01 10:14:44 -070077
78/**
79 * delete the codec and free its resources
80 */
81int AMediaCodec_delete(AMediaCodec*);
82
83/**
84 * Configure the codec. For decoding you would typically get the format from an extractor.
85 */
Marco Nelissen050eb322014-05-09 15:10:23 -070086int AMediaCodec_configure(
87 AMediaCodec*,
88 const AMediaFormat* format,
89 ANativeWindow* surface,
90 AMediaCrypto *crypto,
91 uint32_t flags);
Marco Nelissen0c3be872014-05-01 10:14:44 -070092
93/**
94 * Start the codec. A codec must be configured before it can be started, and must be started
95 * before buffers can be sent to it.
96 */
97int AMediaCodec_start(AMediaCodec*);
98
99/**
100 * Stop the codec.
101 */
102int AMediaCodec_stop(AMediaCodec*);
103
104/*
105 * Flush the codec's input and output. All indices previously returned from calls to
106 * AMediaCodec_dequeueInputBuffer and AMediaCodec_dequeueOutputBuffer become invalid.
107 */
108int AMediaCodec_flush(AMediaCodec*);
109
110/**
111 * Get an input buffer. The specified buffer index must have been previously obtained from
112 * dequeueInputBuffer, and not yet queued.
113 */
114uint8_t* AMediaCodec_getInputBuffer(AMediaCodec*, size_t idx, size_t *out_size);
115
116/**
117 * Get an output buffer. The specified buffer index must have been previously obtained from
118 * dequeueOutputBuffer, and not yet queued.
119 */
120uint8_t* AMediaCodec_getOutputBuffer(AMediaCodec*, size_t idx, size_t *out_size);
121
122/**
123 * Get the index of the next available input buffer. An app will typically use this with
124 * getInputBuffer() to get a pointer to the buffer, then copy the data to be encoded or decoded
125 * into the buffer before passing it to the codec.
126 */
127ssize_t AMediaCodec_dequeueInputBuffer(AMediaCodec*, int64_t timeoutUs);
128
129/**
130 * Send the specified buffer to the codec for processing.
131 */
132int AMediaCodec_queueInputBuffer(AMediaCodec*,
133 size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags);
134
135/**
Marco Nelissen050eb322014-05-09 15:10:23 -0700136 * Send the specified buffer to the codec for processing.
137 */
138int AMediaCodec_queueSecureInputBuffer(AMediaCodec*,
139 size_t idx, off_t offset, AMediaCodecCryptoInfo*, uint64_t time, uint32_t flags);
140
141/**
Marco Nelissen0c3be872014-05-01 10:14:44 -0700142 * Get the index of the next available buffer of processed data.
143 */
144ssize_t AMediaCodec_dequeueOutputBuffer(AMediaCodec*, AMediaCodecBufferInfo *info, int64_t timeoutUs);
145AMediaFormat* AMediaCodec_getOutputFormat(AMediaCodec*);
146
147/**
148 * Release and optionally render the specified buffer.
149 */
150int AMediaCodec_releaseOutputBuffer(AMediaCodec*, size_t idx, bool render);
151
152
Marco Nelissencdb42cd2014-05-08 14:46:05 -0700153typedef void (*OnCodecEvent)(AMediaCodec *codec, void *userdata);
154
155/**
156 * Set a callback to be called when a new buffer is available, or there was a format
157 * or buffer change.
158 * Note that you cannot perform any operations on the mediacodec from within the callback.
159 * If you need to perform mediacodec operations, you must do so on a different thread.
160 */
161int AMediaCodec_setNotificationCallback(AMediaCodec*, OnCodecEvent callback, void *userdata);
162
163
Marco Nelissen050eb322014-05-09 15:10:23 -0700164enum {
165 AMEDIACODECRYPTOINFO_MODE_CLEAR = 0,
166 AMEDIACODECRYPTOINFO_MODE_AES_CTR = 1
167};
168
169/**
170 * create an AMediaCodecCryptoInfo from scratch. Use this if you need to use custom
171 * crypto info, rather than one obtained from AMediaExtractor.
172 */
173AMediaCodecCryptoInfo *AMediaCodecCryptoInfo_new(
174 int numsubsamples,
175 uint8_t key[16],
176 uint8_t iv[16],
177 uint32_t mode,
178 size_t *clearbytes,
179 size_t *encryptedbytes);
180
181/**
Marco Nelissen829e0972014-05-13 16:22:19 -0700182 * delete an AMediaCodecCryptoInfo created previously with AMediaCodecCryptoInfo_new, or
Marco Nelissen050eb322014-05-09 15:10:23 -0700183 * obtained from AMediaExtractor
184 */
185int AMediaCodecCryptoInfo_delete(AMediaCodecCryptoInfo*);
186
187size_t AMediaCodecCryptoInfo_getNumSubSamples(AMediaCodecCryptoInfo*);
188int AMediaCodecCryptoInfo_getKey(AMediaCodecCryptoInfo*, uint8_t *dst);
189int AMediaCodecCryptoInfo_getIV(AMediaCodecCryptoInfo*, uint8_t *dst);
190uint32_t AMediaCodecCryptoInfo_getMode(AMediaCodecCryptoInfo*);
191int AMediaCodecCryptoInfo_getClearBytes(AMediaCodecCryptoInfo*, size_t *dst);
192int AMediaCodecCryptoInfo_getEncryptedBytes(AMediaCodecCryptoInfo*, size_t *dst);
193
Marco Nelissen0c3be872014-05-01 10:14:44 -0700194#ifdef __cplusplus
195} // extern "C"
196#endif
197
198#endif //_NDK_MEDIA_CODEC_H