FFmpeg
Data Structures | Macros | Functions | Variables
mediacodec_wrapper.c File Reference
#include <jni.h>
#include "libavutil/avassert.h"
#include "libavutil/mem.h"
#include "libavutil/avstring.h"
#include "avcodec.h"
#include "ffjni.h"
#include "mediacodec_wrapper.h"

Go to the source code of this file.

Data Structures

struct  JNIAMediaCodecListFields
 
struct  JNIAMediaFormatFields
 
struct  FFAMediaFormat
 
struct  JNIAMediaCodecFields
 
struct  FFAMediaCodec
 

Macros

#define JNI_GET_ENV_OR_RETURN(env, log_ctx, ret)
 
#define JNI_GET_ENV_OR_RETURN_VOID(env, log_ctx)
 
#define CREATE_CODEC_BY_NAME   0
 
#define CREATE_DECODER_BY_TYPE   1
 
#define CREATE_ENCODER_BY_TYPE   2
 
#define DECLARE_FF_AMEDIACODEC_CREATE_FUNC(name, method)
 

Functions

int ff_AMediaCodecProfile_getProfileFromAVCodecContext (AVCodecContext *avctx)
 The following API around MediaCodec and MediaFormat is based on the NDK one provided by Google since Android 5.0. More...
 
char * ff_AMediaCodecList_getCodecNameByType (const char *mime, int profile, int encoder, void *log_ctx)
 
FFAMediaFormat * ff_AMediaFormat_new (void)
 
static FFAMediaFormat * ff_AMediaFormat_newFromObject (void *object)
 
int ff_AMediaFormat_delete (FFAMediaFormat *format)
 
char * ff_AMediaFormat_toString (FFAMediaFormat *format)
 
int ff_AMediaFormat_getInt32 (FFAMediaFormat *format, const char *name, int32_t *out)
 
int ff_AMediaFormat_getInt64 (FFAMediaFormat *format, const char *name, int64_t *out)
 
int ff_AMediaFormat_getFloat (FFAMediaFormat *format, const char *name, float *out)
 
int ff_AMediaFormat_getBuffer (FFAMediaFormat *format, const char *name, void **data, size_t *size)
 
int ff_AMediaFormat_getString (FFAMediaFormat *format, const char *name, const char **out)
 
void ff_AMediaFormat_setInt32 (FFAMediaFormat *format, const char *name, int32_t value)
 
void ff_AMediaFormat_setInt64 (FFAMediaFormat *format, const char *name, int64_t value)
 
void ff_AMediaFormat_setFloat (FFAMediaFormat *format, const char *name, float value)
 
void ff_AMediaFormat_setString (FFAMediaFormat *format, const char *name, const char *value)
 
void ff_AMediaFormat_setBuffer (FFAMediaFormat *format, const char *name, void *data, size_t size)
 
static int codec_init_static_fields (FFAMediaCodec *codec)
 
static FFAMediaCodec * codec_create (int method, const char *arg)
 
int ff_AMediaCodec_delete (FFAMediaCodec *codec)
 
char * ff_AMediaCodec_getName (FFAMediaCodec *codec)
 
int ff_AMediaCodec_configure (FFAMediaCodec *codec, const FFAMediaFormat *format, void *surface, void *crypto, uint32_t flags)
 
int ff_AMediaCodec_start (FFAMediaCodec *codec)
 
int ff_AMediaCodec_stop (FFAMediaCodec *codec)
 
int ff_AMediaCodec_flush (FFAMediaCodec *codec)
 
int ff_AMediaCodec_releaseOutputBuffer (FFAMediaCodec *codec, size_t idx, int render)
 
int ff_AMediaCodec_releaseOutputBufferAtTime (FFAMediaCodec *codec, size_t idx, int64_t timestampNs)
 
ssize_t ff_AMediaCodec_dequeueInputBuffer (FFAMediaCodec *codec, int64_t timeoutUs)
 
int ff_AMediaCodec_queueInputBuffer (FFAMediaCodec *codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
 
ssize_t ff_AMediaCodec_dequeueOutputBuffer (FFAMediaCodec *codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs)
 
uint8_t * ff_AMediaCodec_getInputBuffer (FFAMediaCodec *codec, size_t idx, size_t *out_size)
 
uint8_t * ff_AMediaCodec_getOutputBuffer (FFAMediaCodec *codec, size_t idx, size_t *out_size)
 
FFAMediaFormat * ff_AMediaCodec_getOutputFormat (FFAMediaCodec *codec)
 
int ff_AMediaCodec_infoTryAgainLater (FFAMediaCodec *codec, ssize_t idx)
 
int ff_AMediaCodec_infoOutputBuffersChanged (FFAMediaCodec *codec, ssize_t idx)
 
int ff_AMediaCodec_infoOutputFormatChanged (FFAMediaCodec *codec, ssize_t idx)
 
int ff_AMediaCodec_getBufferFlagCodecConfig (FFAMediaCodec *codec)
 
int ff_AMediaCodec_getBufferFlagEndOfStream (FFAMediaCodec *codec)
 
int ff_AMediaCodec_getBufferFlagKeyFrame (FFAMediaCodec *codec)
 
int ff_AMediaCodec_getConfigureFlagEncode (FFAMediaCodec *codec)
 
int ff_AMediaCodec_cleanOutputBuffers (FFAMediaCodec *codec)
 
int ff_Build_SDK_INT (AVCodecContext *avctx)
 

Variables

static const struct FFJniField jni_amediacodeclist_mapping []
 
static const struct FFJniField jni_amediaformat_mapping []
 
static const AVClass amediaformat_class
 
static const struct FFJniField jni_amediacodec_mapping []
 
static const AVClass amediacodec_class
 

Macro Definition Documentation

◆ JNI_GET_ENV_OR_RETURN

#define JNI_GET_ENV_OR_RETURN (   env,
  log_ctx,
  ret 
)
Value:
do { \
(env) = ff_jni_get_env(log_ctx); \
if (!(env)) { \
return ret; \
} \
} while (0)

Definition at line 300 of file mediacodec_wrapper.c.

◆ JNI_GET_ENV_OR_RETURN_VOID

#define JNI_GET_ENV_OR_RETURN_VOID (   env,
  log_ctx 
)
Value:
do { \
(env) = ff_jni_get_env(log_ctx); \
if (!(env)) { \
return; \
} \
} while (0)

Definition at line 307 of file mediacodec_wrapper.c.

◆ CREATE_CODEC_BY_NAME

#define CREATE_CODEC_BY_NAME   0

Definition at line 1188 of file mediacodec_wrapper.c.

◆ CREATE_DECODER_BY_TYPE

#define CREATE_DECODER_BY_TYPE   1

Definition at line 1189 of file mediacodec_wrapper.c.

◆ CREATE_ENCODER_BY_TYPE

#define CREATE_ENCODER_BY_TYPE   2

Definition at line 1190 of file mediacodec_wrapper.c.

◆ DECLARE_FF_AMEDIACODEC_CREATE_FUNC

#define DECLARE_FF_AMEDIACODEC_CREATE_FUNC (   name,
  method 
)
Value:
FFAMediaCodec *ff_AMediaCodec_##name(const char *arg) \
{ \
return codec_create(method, arg); \
} \

Definition at line 1292 of file mediacodec_wrapper.c.

Function Documentation

◆ ff_AMediaCodecProfile_getProfileFromAVCodecContext()

int ff_AMediaCodecProfile_getProfileFromAVCodecContext ( AVCodecContext *  avctx)

The following API around MediaCodec and MediaFormat is based on the NDK one provided by Google since Android 5.0.

Differences from the NDK API:

Buffers returned by ff_AMediaFormat_toString and ff_AMediaFormat_getString are newly allocated buffer and must be freed by the user after use.

The MediaCrypto API is not implemented.

ff_AMediaCodec_infoTryAgainLater, ff_AMediaCodec_infoOutputBuffersChanged, ff_AMediaCodec_infoOutputFormatChanged, ff_AMediaCodec_cleanOutputBuffers ff_AMediaCodec_getName and ff_AMediaCodec_getBufferFlagEndOfStream are not part of the original NDK API and are convenience functions to hide JNI implementation.

The API around MediaCodecList is not part of the NDK (and is lacking as we still need to retrieve the codec name to work around faulty decoders and encoders).

For documentation, please refers to NdkMediaCodec.h NdkMediaFormat.h and http://developer.android.com/reference/android/media/MediaCodec.html.

Definition at line 314 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_init().

◆ ff_AMediaCodecList_getCodecNameByType()

char* ff_AMediaCodecList_getCodecNameByType ( const char *  mime,
int  profile,
int  encoder,
void *  log_ctx 
)

Definition at line 383 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_init().

◆ ff_AMediaFormat_new()

FFAMediaFormat* ff_AMediaFormat_new ( void  )

Definition at line 625 of file mediacodec_wrapper.c.

Referenced by mediacodec_decode_init().

◆ ff_AMediaFormat_newFromObject()

static FFAMediaFormat* ff_AMediaFormat_newFromObject ( void *  object)
static

Definition at line 670 of file mediacodec_wrapper.c.

Referenced by ff_AMediaCodec_getOutputFormat().

◆ ff_AMediaFormat_delete()

int ff_AMediaFormat_delete ( FFAMediaFormat *  format)

◆ ff_AMediaFormat_toString()

char* ff_AMediaFormat_toString ( FFAMediaFormat *  format)

◆ ff_AMediaFormat_getInt32()

int ff_AMediaFormat_getInt32 ( FFAMediaFormat *  format,
const char *  name,
int32_t *  out 
)

Definition at line 752 of file mediacodec_wrapper.c.

◆ ff_AMediaFormat_getInt64()

int ff_AMediaFormat_getInt64 ( FFAMediaFormat *  format,
const char *  name,
int64_t *  out 
)

Definition at line 791 of file mediacodec_wrapper.c.

◆ ff_AMediaFormat_getFloat()

int ff_AMediaFormat_getFloat ( FFAMediaFormat *  format,
const char *  name,
float *  out 
)

Definition at line 830 of file mediacodec_wrapper.c.

◆ ff_AMediaFormat_getBuffer()

int ff_AMediaFormat_getBuffer ( FFAMediaFormat *  format,
const char *  name,
void **  data,
size_t *  size 
)

Definition at line 869 of file mediacodec_wrapper.c.

◆ ff_AMediaFormat_getString()

int ff_AMediaFormat_getString ( FFAMediaFormat *  format,
const char *  name,
const char **  out 
)

Definition at line 927 of file mediacodec_wrapper.c.

◆ ff_AMediaFormat_setInt32()

void ff_AMediaFormat_setInt32 ( FFAMediaFormat *  format,
const char *  name,
int32_t  value 
)

Definition at line 977 of file mediacodec_wrapper.c.

Referenced by mediacodec_decode_init().

◆ ff_AMediaFormat_setInt64()

void ff_AMediaFormat_setInt64 ( FFAMediaFormat *  format,
const char *  name,
int64_t  value 
)

Definition at line 1002 of file mediacodec_wrapper.c.

◆ ff_AMediaFormat_setFloat()

void ff_AMediaFormat_setFloat ( FFAMediaFormat *  format,
const char *  name,
float  value 
)

Definition at line 1027 of file mediacodec_wrapper.c.

◆ ff_AMediaFormat_setString()

void ff_AMediaFormat_setString ( FFAMediaFormat *  format,
const char *  name,
const char *  value 
)

Definition at line 1052 of file mediacodec_wrapper.c.

Referenced by mediacodec_decode_init().

◆ ff_AMediaFormat_setBuffer()

void ff_AMediaFormat_setBuffer ( FFAMediaFormat *  format,
const char *  name,
void *  data,
size_t  size 
)

Definition at line 1087 of file mediacodec_wrapper.c.

◆ codec_init_static_fields()

static int codec_init_static_fields ( FFAMediaCodec *  codec)
static

Definition at line 1134 of file mediacodec_wrapper.c.

Referenced by codec_create().

◆ codec_create()

static FFAMediaCodec* codec_create ( int  method,
const char *  arg 
)
inlinestatic

Definition at line 1192 of file mediacodec_wrapper.c.

◆ ff_AMediaCodec_delete()

int ff_AMediaCodec_delete ( FFAMediaCodec *  codec)

Definition at line 1302 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_unref().

◆ ff_AMediaCodec_getName()

char* ff_AMediaCodec_getName ( FFAMediaCodec *  codec)

Definition at line 1338 of file mediacodec_wrapper.c.

◆ ff_AMediaCodec_configure()

int ff_AMediaCodec_configure ( FFAMediaCodec *  codec,
const FFAMediaFormat *  format,
void *  surface,
void *  crypto,
uint32_t  flags 
)

Definition at line 1361 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_init().

◆ ff_AMediaCodec_start()

int ff_AMediaCodec_start ( FFAMediaCodec *  codec)

Definition at line 1378 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_init().

◆ ff_AMediaCodec_stop()

int ff_AMediaCodec_stop ( FFAMediaCodec *  codec)

Definition at line 1395 of file mediacodec_wrapper.c.

◆ ff_AMediaCodec_flush()

int ff_AMediaCodec_flush ( FFAMediaCodec *  codec)

Definition at line 1412 of file mediacodec_wrapper.c.

Referenced by mediacodec_dec_flush_codec().

◆ ff_AMediaCodec_releaseOutputBuffer()

int ff_AMediaCodec_releaseOutputBuffer ( FFAMediaCodec *  codec,
size_t  idx,
int  render 
)

◆ ff_AMediaCodec_releaseOutputBufferAtTime()

int ff_AMediaCodec_releaseOutputBufferAtTime ( FFAMediaCodec *  codec,
size_t  idx,
int64_t  timestampNs 
)

Definition at line 1446 of file mediacodec_wrapper.c.

◆ ff_AMediaCodec_dequeueInputBuffer()

ssize_t ff_AMediaCodec_dequeueInputBuffer ( FFAMediaCodec *  codec,
int64_t  timeoutUs 
)

Definition at line 1463 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_send(), and mediacodec_receive_frame().

◆ ff_AMediaCodec_queueInputBuffer()

int ff_AMediaCodec_queueInputBuffer ( FFAMediaCodec *  codec,
size_t  idx,
off_t  offset,
size_t  size,
uint64_t  time,
uint32_t  flags 
)

Definition at line 1480 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_send().

◆ ff_AMediaCodec_dequeueOutputBuffer()

ssize_t ff_AMediaCodec_dequeueOutputBuffer ( FFAMediaCodec *  codec,
FFAMediaCodecBufferInfo *  info,
int64_t  timeoutUs 
)

Definition at line 1497 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_receive().

◆ ff_AMediaCodec_getInputBuffer()

uint8_t* ff_AMediaCodec_getInputBuffer ( FFAMediaCodec *  codec,
size_t  idx,
size_t *  out_size 
)

Definition at line 1532 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_send().

◆ ff_AMediaCodec_getOutputBuffer()

uint8_t* ff_AMediaCodec_getOutputBuffer ( FFAMediaCodec *  codec,
size_t  idx,
size_t *  out_size 
)

Definition at line 1580 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_receive().

◆ ff_AMediaCodec_getOutputFormat()

FFAMediaFormat* ff_AMediaCodec_getOutputFormat ( FFAMediaCodec *  codec)

Definition at line 1628 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_init(), and ff_mediacodec_dec_receive().

◆ ff_AMediaCodec_infoTryAgainLater()

int ff_AMediaCodec_infoTryAgainLater ( FFAMediaCodec *  codec,
ssize_t  idx 
)

Definition at line 1651 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_receive(), and ff_mediacodec_dec_send().

◆ ff_AMediaCodec_infoOutputBuffersChanged()

int ff_AMediaCodec_infoOutputBuffersChanged ( FFAMediaCodec *  codec,
ssize_t  idx 
)

Definition at line 1656 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_receive().

◆ ff_AMediaCodec_infoOutputFormatChanged()

int ff_AMediaCodec_infoOutputFormatChanged ( FFAMediaCodec *  codec,
ssize_t  idx 
)

Definition at line 1661 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_receive().

◆ ff_AMediaCodec_getBufferFlagCodecConfig()

int ff_AMediaCodec_getBufferFlagCodecConfig ( FFAMediaCodec *  codec)

Definition at line 1666 of file mediacodec_wrapper.c.

◆ ff_AMediaCodec_getBufferFlagEndOfStream()

int ff_AMediaCodec_getBufferFlagEndOfStream ( FFAMediaCodec *  codec)

Definition at line 1671 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_receive(), and ff_mediacodec_dec_send().

◆ ff_AMediaCodec_getBufferFlagKeyFrame()

int ff_AMediaCodec_getBufferFlagKeyFrame ( FFAMediaCodec *  codec)

Definition at line 1676 of file mediacodec_wrapper.c.

◆ ff_AMediaCodec_getConfigureFlagEncode()

int ff_AMediaCodec_getConfigureFlagEncode ( FFAMediaCodec *  codec)

Definition at line 1681 of file mediacodec_wrapper.c.

◆ ff_AMediaCodec_cleanOutputBuffers()

int ff_AMediaCodec_cleanOutputBuffers ( FFAMediaCodec *  codec)

Definition at line 1686 of file mediacodec_wrapper.c.

Referenced by ff_mediacodec_dec_receive().

◆ ff_Build_SDK_INT()

int ff_Build_SDK_INT ( AVCodecContext *  avctx)

Definition at line 1709 of file mediacodec_wrapper.c.

Referenced by mediacodec_decode_init().

Variable Documentation

◆ jni_amediacodeclist_mapping

const struct FFJniField jni_amediacodeclist_mapping[]
static

◆ jni_amediaformat_mapping

const struct FFJniField jni_amediaformat_mapping[]
static
Initial value:
= {
{ "android/media/MediaFormat", NULL, NULL, FF_JNI_CLASS, 0x42, 1 },
{ "android/media/MediaFormat", "<init>", "()V", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "containsKey", "(Ljava/lang/String;)Z", FF_JNI_METHOD,0x42, 1 },
{ "android/media/MediaFormat", "getInteger", "(Ljava/lang/String;)I", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "getLong", "(Ljava/lang/String;)J", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "getFloat", "(Ljava/lang/String;)F", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "getByteBuffer", "(Ljava/lang/String;)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "getString", "(Ljava/lang/String;)Ljava/lang/String;", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "setInteger", "(Ljava/lang/String;I)V", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "setLong", "(Ljava/lang/String;J)V", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "setFloat", "(Ljava/lang/String;F)V", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "setString", "(Ljava/lang/String;Ljava/lang/String;)V", FF_JNI_METHOD, 0x42, 1 },
{ "android/media/MediaFormat", "toString", "()Ljava/lang/String;", FF_JNI_METHOD, 0x42, 1 },
{ NULL }
}

Definition at line 133 of file mediacodec_wrapper.c.

Referenced by ff_AMediaCodecList_getCodecNameByType(), ff_AMediaFormat_delete(), ff_AMediaFormat_new(), and ff_AMediaFormat_newFromObject().

◆ amediaformat_class

const AVClass amediaformat_class
static
Initial value:
= {
.class_name = "amediaformat",
.item_name = av_default_item_name,
}

Definition at line 157 of file mediacodec_wrapper.c.

Referenced by ff_AMediaFormat_new(), and ff_AMediaFormat_newFromObject().

◆ jni_amediacodec_mapping

const struct FFJniField jni_amediacodec_mapping[]
static

Definition at line 220 of file mediacodec_wrapper.c.

Referenced by codec_create(), and ff_AMediaCodec_delete().

◆ amediacodec_class

const AVClass amediacodec_class
static
Initial value:
= {
.class_name = "amediacodec",
.item_name = av_default_item_name,
}

Definition at line 269 of file mediacodec_wrapper.c.

Referenced by codec_create().

name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
FF_JNI_CLASS
@ FF_JNI_CLASS
Definition: ffjni.h:90
arg
const char * arg
Definition: jacosubdec.c:67
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
FF_JNI_METHOD
@ FF_JNI_METHOD
Definition: ffjni.h:93
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
ff_jni_get_env
JNIEnv * ff_jni_get_env(void *log_ctx)
Definition: ffjni.c:52
FFAMediaCodec
Definition: mediacodec_wrapper.c:275
ret
ret
Definition: filter_design.txt:187
codec_create
static FFAMediaCodec * codec_create(int method, const char *arg)
Definition: mediacodec_wrapper.c:1192