1/*
2 * Android MediaCodec Wrapper
3 *
4 * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef AVCODEC_MEDIACODEC_WRAPPER_H
24#define AVCODEC_MEDIACODEC_WRAPPER_H
25
26#include <stdint.h>
27#include <sys/types.h>
28
29#include "avcodec.h"
30
31/**
32 * The following API around MediaCodec and MediaFormat is based on the
33 * NDK one provided by Google since Android 5.0.
34 *
35 * Differences from the NDK API:
36 *
37 * Buffers returned by ff_AMediaFormat_toString and ff_AMediaFormat_getString
38 * are newly allocated buffer and must be freed by the user after use.
39 *
40 * The MediaCrypto API is not implemented.
41 *
42 * ff_AMediaCodec_infoTryAgainLater, ff_AMediaCodec_infoOutputBuffersChanged,
43 * ff_AMediaCodec_infoOutputFormatChanged, ff_AMediaCodec_cleanOutputBuffers
44 * ff_AMediaCodec_getName and ff_AMediaCodec_getBufferFlagEndOfStream are not
45 * part of the original NDK API and are convenience functions to hide JNI
46 * implementation.
47 *
48 * The API around MediaCodecList is not part of the NDK (and is lacking as
49 * we still need to retrieve the codec name to work around faulty decoders
50 * and encoders).
51 *
52 * For documentation, please refers to NdkMediaCodec.h NdkMediaFormat.h and
53 * http://developer.android.com/reference/android/media/MediaCodec.html.
54 *
55 */
56
57int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx);
58
59char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx);
60
61struct FFAMediaFormat;
62typedef struct FFAMediaFormat FFAMediaFormat;
63
64FFAMediaFormat *ff_AMediaFormat_new(void);
65int ff_AMediaFormat_delete(FFAMediaFormat* format);
66
67char* ff_AMediaFormat_toString(FFAMediaFormat* format);
68
69int ff_AMediaFormat_getInt32(FFAMediaFormat* format, const char *name, int32_t *out);
70int ff_AMediaFormat_getInt64(FFAMediaFormat* format, const char *name, int64_t *out);
71int ff_AMediaFormat_getFloat(FFAMediaFormat* format, const char *name, float *out);
72int ff_AMediaFormat_getBuffer(FFAMediaFormat* format, const char *name, void** data, size_t *size);
73int ff_AMediaFormat_getString(FFAMediaFormat* format, const char *name, const char **out);
74
75void ff_AMediaFormat_setInt32(FFAMediaFormat* format, const char* name, int32_t value);
76void ff_AMediaFormat_setInt64(FFAMediaFormat* format, const char* name, int64_t value);
77void ff_AMediaFormat_setFloat(FFAMediaFormat* format, const char* name, float value);
78void ff_AMediaFormat_setString(FFAMediaFormat* format, const char* name, const char* value);
79void ff_AMediaFormat_setBuffer(FFAMediaFormat* format, const char* name, void* data, size_t size);
80
81struct FFAMediaCodec;
82typedef struct FFAMediaCodec FFAMediaCodec;
83typedef struct FFAMediaCodecCryptoInfo FFAMediaCodecCryptoInfo;
84
85struct FFAMediaCodecBufferInfo {
86    int32_t offset;
87    int32_t size;
88    int64_t presentationTimeUs;
89    uint32_t flags;
90};
91typedef struct FFAMediaCodecBufferInfo FFAMediaCodecBufferInfo;
92
93char *ff_AMediaCodec_getName(FFAMediaCodec *codec);
94
95FFAMediaCodec* ff_AMediaCodec_createCodecByName(const char *name);
96FFAMediaCodec* ff_AMediaCodec_createDecoderByType(const char *mime_type);
97FFAMediaCodec* ff_AMediaCodec_createEncoderByType(const char *mime_type);
98
99int ff_AMediaCodec_configure(FFAMediaCodec* codec, const FFAMediaFormat* format, void* surface, void *crypto, uint32_t flags);
100int ff_AMediaCodec_start(FFAMediaCodec* codec);
101int ff_AMediaCodec_stop(FFAMediaCodec* codec);
102int ff_AMediaCodec_flush(FFAMediaCodec* codec);
103int ff_AMediaCodec_delete(FFAMediaCodec* codec);
104
105uint8_t* ff_AMediaCodec_getInputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size);
106uint8_t* ff_AMediaCodec_getOutputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size);
107
108ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec* codec, int64_t timeoutUs);
109int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec* codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags);
110
111ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec* codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs);
112FFAMediaFormat* ff_AMediaCodec_getOutputFormat(FFAMediaCodec* codec);
113
114int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec* codec, size_t idx, int render);
115int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs);
116
117int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx);
118int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx);
119int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t indx);
120
121int ff_AMediaCodec_getBufferFlagCodecConfig (FFAMediaCodec *codec);
122int ff_AMediaCodec_getBufferFlagEndOfStream(FFAMediaCodec *codec);
123int ff_AMediaCodec_getBufferFlagKeyFrame(FFAMediaCodec *codec);
124
125int ff_AMediaCodec_getConfigureFlagEncode(FFAMediaCodec *codec);
126
127int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec);
128
129int ff_Build_SDK_INT(AVCodecContext *avctx);
130
131#endif /* AVCODEC_MEDIACODEC_WRAPPER_H */
132