Commit cae9e8d8 authored by Thomas Guillem's avatar Thomas Guillem
Browse files

mediacodec: add JNI module

Add the mc_api struct, used by Decoder to access a MediaCodec API.
parent ac4a4aee
......@@ -397,7 +397,8 @@ libiomx_plugin_la_CPPFLAGS = $(libomxil_plugin_la_CPPFLAGS) -DUSE_IOMX
libiomx_plugin_la_LIBADD = $(libomxil_plugin_la_LIBADD)
libmediacodec_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/codec/omxil
libmediacodec_plugin_la_SOURCES = codec/omxil/mediacodec.c codec/omxil/utils.c \
libmediacodec_plugin_la_SOURCES = codec/omxil/mediacodec.c codec/omxil/mediacodec.h \
codec/omxil/mediacodec_jni.c codec/omxil/utils.c \
video_chroma/copy.c codec/omxil/android_opaque.c codec/omxil/android_opaque.h \
packetizer/h264_nal.c packetizer/h264_nal.h
packetizer/hevc_nal.c packetizer/hevc_nal.h
......
......@@ -38,6 +38,7 @@
#include <vlc_cpu.h>
#include <vlc_memory.h>
#include "mediacodec.h"
#include "../../packetizer/h264_nal.h"
#include "../../packetizer/hevc_nal.h"
#include <OMX_Core.h>
......@@ -46,15 +47,6 @@
#include "android_opaque.h"
#include "../../video_output/android/android_window.h"
#define BUFFER_FLAG_CODEC_CONFIG 2
#define INFO_OUTPUT_BUFFERS_CHANGED -3
#define INFO_OUTPUT_FORMAT_CHANGED -2
#define INFO_TRY_AGAIN_LATER -1
#define THREAD_NAME "android_mediacodec"
extern JNIEnv *jni_get_env(const char *name);
/* JNI functions to get/set an Android Surface object. */
extern jobject jni_LockAndGetAndroidJavaSurface();
extern void jni_UnlockAndroidSurface();
......@@ -141,46 +133,14 @@ struct csd
size_t i_size;
};
struct mc_out
{
enum {
MC_OUT_TYPE_BUF,
MC_OUT_TYPE_CONF,
} type;
union
{
struct
{
int i_index;
mtime_t i_ts;
const void *p_ptr;
int i_size;
} buf;
struct
{
int width, height;
int stride;
int slice_height;
int pixel_format;
int crop_left;
int crop_top;
int crop_right;
int crop_bottom;
} conf;
} u;
};
struct decoder_sys_t
{
mc_api *api;
uint32_t nal_size;
jobject codec;
jobject buffer_info;
jobject input_buffers, output_buffers;
int pixel_format;
int stride, slice_height;
const char *mime;
char *name;
/* Codec Specific Data buffer: sent in PutInput after a start or a flush
* with the BUFFER_FLAG_CODEC_CONFIG flag.*/
......@@ -192,11 +152,10 @@ struct decoder_sys_t
int i_width;
int i_height;
bool allocated;
bool started;
bool decoded;
bool error_state;
bool b_new_block;
bool b_support_interlaced;
ArchitectureSpecificCopyData architecture_specific_data;
......@@ -210,105 +169,11 @@ struct decoder_sys_t
int64_t i_preroll_end;
};
struct jfields
{
jclass media_codec_list_class, media_codec_class, media_format_class;
jclass buffer_info_class, byte_buffer_class;
jmethodID tostring;
jmethodID get_codec_count, get_codec_info_at, is_encoder, get_capabilities_for_type;
jfieldID profile_levels_field, profile_field, level_field;
jmethodID get_supported_types, get_name;
jmethodID create_by_codec_name, configure, start, stop, flush, release;
jmethodID get_output_format;
jmethodID get_input_buffers, get_input_buffer;
jmethodID get_output_buffers, get_output_buffer;
jmethodID dequeue_input_buffer, dequeue_output_buffer, queue_input_buffer;
jmethodID release_output_buffer;
jmethodID create_video_format, set_integer, set_bytebuffer, get_integer;
jmethodID buffer_info_ctor;
jfieldID size_field, offset_field, pts_field;
};
static struct jfields jfields;
enum Types
{
METHOD, STATIC_METHOD, FIELD
};
#define OFF(x) offsetof(struct jfields, x)
struct classname
{
const char *name;
int offset;
};
static const struct classname classes[] = {
{ "android/media/MediaCodecList", OFF(media_codec_list_class) },
{ "android/media/MediaCodec", OFF(media_codec_class) },
{ "android/media/MediaFormat", OFF(media_format_class) },
{ "android/media/MediaFormat", OFF(media_format_class) },
{ "android/media/MediaCodec$BufferInfo", OFF(buffer_info_class) },
{ "java/nio/ByteBuffer", OFF(byte_buffer_class) },
{ NULL, 0 },
};
struct member
{
const char *name;
const char *sig;
const char *class;
int offset;
int type;
bool critical;
};
static const struct member members[] = {
{ "toString", "()Ljava/lang/String;", "java/lang/Object", OFF(tostring), METHOD, true },
{ "getCodecCount", "()I", "android/media/MediaCodecList", OFF(get_codec_count), STATIC_METHOD, true },
{ "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", "android/media/MediaCodecList", OFF(get_codec_info_at), STATIC_METHOD, true },
{ "isEncoder", "()Z", "android/media/MediaCodecInfo", OFF(is_encoder), METHOD, true },
{ "getSupportedTypes", "()[Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_supported_types), METHOD, true },
{ "getName", "()Ljava/lang/String;", "android/media/MediaCodecInfo", OFF(get_name), METHOD, true },
{ "getCapabilitiesForType", "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;", "android/media/MediaCodecInfo", OFF(get_capabilities_for_type), METHOD, true },
{ "profileLevels", "[Landroid/media/MediaCodecInfo$CodecProfileLevel;", "android/media/MediaCodecInfo$CodecCapabilities", OFF(profile_levels_field), FIELD, true },
{ "profile", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(profile_field), FIELD, true },
{ "level", "I", "android/media/MediaCodecInfo$CodecProfileLevel", OFF(level_field), FIELD, true },
{ "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", "android/media/MediaCodec", OFF(create_by_codec_name), STATIC_METHOD, true },
{ "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", "android/media/MediaCodec", OFF(configure), METHOD, true },
{ "start", "()V", "android/media/MediaCodec", OFF(start), METHOD, true },
{ "stop", "()V", "android/media/MediaCodec", OFF(stop), METHOD, true },
{ "flush", "()V", "android/media/MediaCodec", OFF(flush), METHOD, true },
{ "release", "()V", "android/media/MediaCodec", OFF(release), METHOD, true },
{ "getOutputFormat", "()Landroid/media/MediaFormat;", "android/media/MediaCodec", OFF(get_output_format), METHOD, true },
{ "getInputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_input_buffers), METHOD, false },
{ "getInputBuffer", "(I)Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_input_buffer), METHOD, false },
{ "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_output_buffers), METHOD, false },
{ "getOutputBuffer", "(I)Ljava/nio/ByteBuffer;", "android/media/MediaCodec", OFF(get_output_buffer), METHOD, false },
{ "dequeueInputBuffer", "(J)I", "android/media/MediaCodec", OFF(dequeue_input_buffer), METHOD, true },
{ "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", "android/media/MediaCodec", OFF(dequeue_output_buffer), METHOD, true },
{ "queueInputBuffer", "(IIIJI)V", "android/media/MediaCodec", OFF(queue_input_buffer), METHOD, true },
{ "releaseOutputBuffer", "(IZ)V", "android/media/MediaCodec", OFF(release_output_buffer), METHOD, true },
{ "createVideoFormat", "(Ljava/lang/String;II)Landroid/media/MediaFormat;", "android/media/MediaFormat", OFF(create_video_format), STATIC_METHOD, true },
{ "setInteger", "(Ljava/lang/String;I)V", "android/media/MediaFormat", OFF(set_integer), METHOD, true },
{ "getInteger", "(Ljava/lang/String;)I", "android/media/MediaFormat", OFF(get_integer), METHOD, true },
{ "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", "android/media/MediaFormat", OFF(set_bytebuffer), METHOD, true },
{ "<init>", "()V", "android/media/MediaCodec$BufferInfo", OFF(buffer_info_ctor), METHOD, true },
{ "size", "I", "android/media/MediaCodec$BufferInfo", OFF(size_field), FIELD, true },
{ "offset", "I", "android/media/MediaCodec$BufferInfo", OFF(offset_field), FIELD, true },
{ "presentationTimeUs", "J", "android/media/MediaCodec$BufferInfo", OFF(pts_field), FIELD, true },
{ NULL, NULL, NULL, 0, 0, false },
};
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int OpenDecoder(vlc_object_t *);
static int OpenDecoderJni(vlc_object_t *);
static void CloseDecoder(vlc_object_t *);
static void JniStopMediaCodec(decoder_t *p_dec, JNIEnv *env);
static picture_t *DecodeVideo(decoder_t *, block_t **);
......@@ -332,117 +197,10 @@ vlc_module_begin ()
set_capability( "decoder", 0 ) /* Only enabled via commandline arguments */
add_bool(CFG_PREFIX "dr", true,
DIRECTRENDERING_TEXT, DIRECTRENDERING_LONGTEXT, true)
set_callbacks( OpenDecoder, CloseDecoder )
set_callbacks( OpenDecoderJni, CloseDecoder )
add_shortcut( "mediacodec_jni" )
vlc_module_end ()
static inline int get_integer(JNIEnv *env, jobject obj, const char *psz_name)
{
int i_ret;
jstring jname = (*env)->NewStringUTF(env, psz_name);
i_ret = (*env)->CallIntMethod(env, obj, jfields.get_integer, jname);
(*env)->DeleteLocalRef(env, jname);
return i_ret;
}
#define GET_INTEGER(obj, name) get_integer(env, obj, name)
static int jstrcmp(JNIEnv* env, jobject str, const char* str2)
{
jsize len = (*env)->GetStringUTFLength(env, str);
if (len != (jsize) strlen(str2))
return -1;
const char *ptr = (*env)->GetStringUTFChars(env, str, NULL);
int ret = memcmp(ptr, str2, len);
(*env)->ReleaseStringUTFChars(env, str, ptr);
return ret;
}
static inline bool check_exception( JNIEnv *env )
{
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionClear(env);
return true;
}
else
return false;
}
#define CHECK_EXCEPTION() check_exception( env )
/* Initialize all jni fields.
* Done only one time during the first initialisation */
static bool
InitJNIFields( decoder_t *p_dec, JNIEnv *env )
{
static vlc_mutex_t lock = VLC_STATIC_MUTEX;
static int i_init_state = -1;
bool ret;
vlc_mutex_lock( &lock );
if( i_init_state != -1 )
goto end;
i_init_state = 0;
for (int i = 0; classes[i].name; i++) {
jclass clazz = (*env)->FindClass(env, classes[i].name);
if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Unable to find class %s", classes[i].name);
goto end;
}
*(jclass*)((uint8_t*)&jfields + classes[i].offset) =
(jclass) (*env)->NewGlobalRef(env, clazz);
}
jclass last_class;
for (int i = 0; members[i].name; i++) {
if (i == 0 || strcmp(members[i].class, members[i - 1].class))
last_class = (*env)->FindClass(env, members[i].class);
if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Unable to find class %s", members[i].class);
goto end;
}
switch (members[i].type) {
case METHOD:
*(jmethodID*)((uint8_t*)&jfields + members[i].offset) =
(*env)->GetMethodID(env, last_class, members[i].name, members[i].sig);
break;
case STATIC_METHOD:
*(jmethodID*)((uint8_t*)&jfields + members[i].offset) =
(*env)->GetStaticMethodID(env, last_class, members[i].name, members[i].sig);
break;
case FIELD:
*(jfieldID*)((uint8_t*)&jfields + members[i].offset) =
(*env)->GetFieldID(env, last_class, members[i].name, members[i].sig);
break;
}
if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Unable to find the member %s in %s",
members[i].name, members[i].class);
if (members[i].critical)
goto end;
}
}
/* getInputBuffers and getOutputBuffers are deprecated if API >= 21
* use getInputBuffer and getOutputBuffer instead. */
if (jfields.get_input_buffer && jfields.get_output_buffer) {
jfields.get_output_buffers =
jfields.get_input_buffers = NULL;
} else if (!jfields.get_output_buffers && !jfields.get_input_buffers) {
msg_Err(p_dec, "Unable to find get Output/Input Buffer/Buffers");
goto end;
}
i_init_state = 1;
end:
ret = i_init_state == 1;
if( !ret )
msg_Err( p_dec, "MediaCodec jni init failed" );
vlc_mutex_unlock( &lock );
return ret;
}
static void CSDFree(decoder_t *p_dec)
{
......@@ -567,252 +325,10 @@ static int H264SetCSD(decoder_t *p_dec, void *p_buf, size_t i_size,
return VLC_EGENERIC;
}
static jstring GetMediaCodecName(decoder_t *p_dec, JNIEnv *env,
const char *mime, jstring jmime,
size_t h264_profile)
{
decoder_sys_t *p_sys = p_dec->p_sys;
int num_codecs;
jstring jcodec_name = NULL;
num_codecs = (*env)->CallStaticIntMethod(env,
jfields.media_codec_list_class,
jfields.get_codec_count);
for (int i = 0; i < num_codecs; i++)
{
jobject codec_capabilities = NULL;
jobject profile_levels = NULL;
jobject info = NULL;
jobject name = NULL;
jobject types = NULL;
jsize name_len = 0;
int profile_levels_len = 0, num_types = 0;
const char *name_ptr = NULL;
bool found = false;
info = (*env)->CallStaticObjectMethod(env, jfields.media_codec_list_class,
jfields.get_codec_info_at, i);
name = (*env)->CallObjectMethod(env, info, jfields.get_name);
name_len = (*env)->GetStringUTFLength(env, name);
name_ptr = (*env)->GetStringUTFChars(env, name, NULL);
if (OMXCodec_IsBlacklisted( name_ptr, name_len))
goto loopclean;
if ((*env)->CallBooleanMethod(env, info, jfields.is_encoder))
goto loopclean;
codec_capabilities = (*env)->CallObjectMethod(env, info,
jfields.get_capabilities_for_type,
jmime);
if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodecInfo.getCapabilitiesForType");
goto loopclean;
} else if (codec_capabilities) {
profile_levels = (*env)->GetObjectField(env, codec_capabilities, jfields.profile_levels_field);
if (profile_levels)
profile_levels_len = (*env)->GetArrayLength(env, profile_levels);
}
msg_Dbg(p_dec, "Number of profile levels: %d", profile_levels_len);
types = (*env)->CallObjectMethod(env, info, jfields.get_supported_types);
num_types = (*env)->GetArrayLength(env, types);
found = false;
for (int j = 0; j < num_types && !found; j++) {
jobject type = (*env)->GetObjectArrayElement(env, types, j);
if (!jstrcmp(env, type, mime)) {
/* The mime type is matching for this component. We
now check if the capabilities of the codec is
matching the video format. */
if (h264_profile) {
/* This decoder doesn't expose its profiles and is high
* profile capable */
if (!strncmp(name_ptr, "OMX.LUMEVideoDecoder", __MIN(20, name_len)))
found = true;
for (int i = 0; i < profile_levels_len && !found; ++i) {
jobject profile_level = (*env)->GetObjectArrayElement(env, profile_levels, i);
int omx_profile = (*env)->GetIntField(env, profile_level, jfields.profile_field);
size_t codec_profile = convert_omx_to_profile_idc(omx_profile);
(*env)->DeleteLocalRef(env, profile_level);
if (codec_profile != h264_profile)
continue;
/* Some encoders set the level too high, thus we ignore it for the moment.
We could try to guess the actual profile based on the resolution. */
found = true;
}
}
else
found = true;
}
(*env)->DeleteLocalRef(env, type);
}
if (found) {
msg_Dbg(p_dec, "using %.*s", name_len, name_ptr);
p_sys->name = malloc(name_len + 1);
memcpy(p_sys->name, name_ptr, name_len);
p_sys->name[name_len] = '\0';
jcodec_name = name;
}
loopclean:
if (name)
{
(*env)->ReleaseStringUTFChars(env, name, name_ptr);
if (jcodec_name != name)
(*env)->DeleteLocalRef(env, name);
}
if (profile_levels)
(*env)->DeleteLocalRef(env, profile_levels);
if (types)
(*env)->DeleteLocalRef(env, types);
if (codec_capabilities)
(*env)->DeleteLocalRef(env, codec_capabilities);
if (info)
(*env)->DeleteLocalRef(env, info);
if (found)
break;
}
return jcodec_name;
}
static int JniStartMediaCodec(decoder_t *p_dec, JNIEnv *env, jobject jsurface,
const char *mime, int i_width, int i_height,
size_t h264_profile, int i_angle)
{
decoder_sys_t *p_sys = p_dec->p_sys;
int i_ret = VLC_EGENERIC;
jstring jmime = NULL;
jstring jcodec_name = NULL;
jobject jcodec = NULL;
jobject jformat = NULL;
jstring jrotation_string = NULL;
jobject jinput_buffers = NULL;
jobject joutput_buffers = NULL;
jobject jbuffer_info = NULL;
jmime = (*env)->NewStringUTF(env, mime);
if (!jmime)
return VLC_EGENERIC;
jcodec_name = GetMediaCodecName(p_dec, env, mime, jmime, h264_profile);
if (!jcodec_name) {
msg_Dbg(p_dec, "No suitable codec matching %s was found", mime);
goto error;
}
// This method doesn't handle errors nicely, it crashes if the codec isn't found.
// (The same goes for createDecoderByType.) This is fixed in latest AOSP and in 4.2,
// but not in 4.1 devices.
jcodec = (*env)->CallStaticObjectMethod(env, jfields.media_codec_class,
jfields.create_by_codec_name,
jcodec_name);
if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodec.createByCodecName.");
goto error;
}
p_sys->allocated = true;
p_sys->codec = (*env)->NewGlobalRef(env, jcodec);
jformat = (*env)->CallStaticObjectMethod(env, jfields.media_format_class,
jfields.create_video_format, jmime,
i_width, i_height);
p_sys->direct_rendering = !!jsurface;
/* There is no way to rotate the video using direct rendering (and using a
* SurfaceView) before API 21 (Lollipop). Therefore, we deactivate direct
* rendering if video doesn't have a normal rotation and if
* get_input_buffer method is not present (This method exists since API
* 21). */
if (p_sys->direct_rendering && i_angle != 0 && !jfields.get_input_buffer)
p_sys->direct_rendering = false;
if (p_sys->direct_rendering) {
if (i_angle != 0)
{
jrotation_string = (*env)->NewStringUTF(env, "rotation-degrees");
(*env)->CallVoidMethod(env, jformat, jfields.set_integer,
jrotation_string, i_angle);
}
// Configure MediaCodec with the Android surface.
(*env)->CallVoidMethod(env, p_sys->codec, jfields.configure,
jformat, jsurface, NULL, 0);
if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodec.configure with an output surface.");
goto error;
}
} else {
(*env)->CallVoidMethod(env, p_sys->codec, jfields.configure,
jformat, NULL, NULL, 0);
if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodec.configure");
goto error;
}
}
(*env)->CallVoidMethod(env, p_sys->codec, jfields.start);
if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodec.start");
goto error;
}
p_sys->started = true;
if (jfields.get_input_buffers && jfields.get_output_buffers) {
jinput_buffers = (*env)->CallObjectMethod(env, p_sys->codec,
jfields.get_input_buffers);
if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception in MediaCodec.getInputBuffers (OpenDecoder)");
goto error;
}
p_sys->input_buffers = (*env)->NewGlobalRef(env, jinput_buffers);
joutput_buffers = (*env)->CallObjectMethod(env, p_sys->codec,
jfields.get_output_buffers);
if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception in MediaCodec.getOutputBuffers (OpenDecoder)");
goto error;
}
p_sys->output_buffers = (*env)->NewGlobalRef(env, joutput_buffers);
}
jbuffer_info = (*env)->NewObject(env, jfields.buffer_info_class,
jfields.buffer_info_ctor);
p_sys->buffer_info = (*env)->NewGlobalRef(env, jbuffer_info);
i_ret = VLC_SUCCESS;
error:
if (jmime)
(*env)->DeleteLocalRef(env, jmime);
if (jcodec_name)
(*env)->DeleteLocalRef(env, jcodec_name);
if (jcodec)
(*env)->DeleteLocalRef(env, jcodec);
if (jformat)
(*env)->DeleteLocalRef(env, jformat);
if (jrotation_string)
(*env)->DeleteLocalRef(env, jrotation_string);
if (jinput_buffers)
(*env)->DeleteLocalRef(env, jinput_buffers);
if (joutput_buffers)
(*env)->DeleteLocalRef(env, joutput_buffers);
if (jbuffer_info)
(*env)->DeleteLocalRef(env, jbuffer_info);
if (i_ret != VLC_SUCCESS)
JniStopMediaCodec(p_dec, env);
return i_ret;
}
/*****************************************************************************
* StartMediaCodec: Create the mediacodec instance
*****************************************************************************/
static int StartMediaCodec(decoder_t *p_dec, JNIEnv *env)
static int StartMediaCodec(decoder_t *p_dec)
{
decoder_sys_t *p_sys = p_dec->p_sys;
int i_angle = 0, i_ret;
......@@ -898,15 +414,14 @@ static int StartMediaCodec(decoder_t *p_dec, JNIEnv *env)
if (var_InheritBool(p_dec, CFG_PREFIX "dr"))
jsurface = jni_LockAndGetAndroidJavaSurface();
i_ret = JniStartMediaCodec(p_dec, env, jsurface, p_sys->mime,
p_sys->i_width, p_sys->i_height, h264_profile,
i_angle);
i_ret = p_sys->api->start(p_sys->api, jsurface, p_sys->mime, p_sys->i_width,
p_sys->i_height, h264_profile, i_angle);
if (jsurface)
jni_UnlockAndroidSurface();
if (i_ret == VLC_SUCCESS)
{
if (p_sys->direct_rendering)
if (p_sys->api->b_direct_rendering)
p_dec->fmt_out.i_codec = VLC_CODEC_ANDROID_OPAQUE;
p_sys->b_update_format = true;
return VLC_SUCCESS;
......@@ -914,69 +429,28 @@ static int StartMediaCodec(decoder_t *p_dec, JNIEnv *env)
return VLC_EGENERIC;
}
static void JniStopMediaCodec(decoder_t *p_dec, JNIEnv *env)
{
decoder_sys_t *p_sys = p_dec->p_sys;