FFmpeg and his "h264_mediacodec" codec -> "No Java virtual machine has been registered"

1,880 views
Skip to first unread message

Antoine LAVIER

unread,
Nov 22, 2016, 9:10:56 AM11/22/16
to javacpp
Hi!

I use Javacpp 1.2.3 and FFmpeg preset 1.2 (with FFmpeg 3.1.4). I want to use FFmpeg with the (java/native) Android mediacodec decoder to analyse performance difference.
So I compiled FFmpeg (with your preset at the actual "master" branch) after modifying cppbuild.sh (in ffmpeg directory) by adding "--enable-jni --enable-mediacodec" for FFmpeg configure command (for arch(s) arm and x86).
All works fine, but when I force my app to use mediacodec (doing "codec = avcodec_find_decoder_by_name("h246_mediacodec");"), I've got these FFmpeg logs :


[amediaformat @ 0xde49c8c0] No Java virtual machine has been registered
[h264_mediacodec @ 0xea32a800] Failed to create media format

Then my app fails to open codec. I tested it on Android 23 and Android 18, same results. Looking closer in FFmpeg source code, I found "ffjni.c" with a function "ff_jni_attach_env(...)". Any idea how to use it ? I can't find that function in javacpp ffmpeg preset class files.

Thanks!

Antoine LAVIER

unread,
Nov 22, 2016, 9:57:51 AM11/22/16
to javacpp
EDIT: Should I call "av_jni_set_java_vm(void* vm, void* log_ctx)" from "jni.h"? If yes, how?

Antoine LAVIER

unread,
Nov 23, 2016, 4:45:19 AM11/23/16
to javacpp
EDIT2: After reading that : https://ffmpeg.org/pipermail/ffmpeg-devel/2015-October/180928.html, I sounds like I should call "av_jni_set_java_vm(void* vm, void* log_ctx)" int the "JNI_OnLoad()" method. How can add/overload this method?

Samuel Audet

unread,
Nov 23, 2016, 5:02:10 AM11/23/16
to javacpp...@googlegroups.com
This JNI functionality has nothing to do with JavaCPP. According to that
message on the mailing list of FFmpeg, these features are there to be
able to tap into Android's subsystem from the native side, instead of
doing it from Java, which is much easier, but maybe less efficient yes...

Anyway, if you could explain what you are trying to do, I might be able
to help!

Samuel

Antoine LAVIER

unread,
Nov 23, 2016, 5:36:18 AM11/23/16
to javacpp
I understand the abstraction level of JavaCPP on JNI.

Actually, after a lot of research, I think that the Android MediaCodec API (SDK and NDK)
is more efficient than FFmpeg to process images on an Android device because it uses some
hardware acceleration. I saw that FFmpeg makes its "wrapper" of Android MediaCodec and I
would like to test it and compare with my current FFmpeg default codec. Problem is with
MediaCodec (provided from Android SDK/NDK) app is limited to a maximum number of simultaneous
frames (possibly limited by frames resolution) whereas FFmpeg uses it as a codec like another.

Now, I'm trying to make a simple cpp program with this code:

#include <jni.h>

#include "libavcodec/jni.h"

jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
av_jni_set_java_vm(vm, reserved);
return JNI_VERSION_1_6;
}

Then, before open a codec in my Java code, I'll try to call "System.loadLibrary(/*myLib*/)". Do you think it's a proper way to init JavaVM as FFmpeg expected ?
Thanks for your help!

Antoine

Antoine LAVIER

unread,
Nov 23, 2016, 9:22:24 AM11/23/16
to javacpp
Refer to my previous answer, I've lost all FFmpeg logs with my manipulation (it doesn't work). Question is: how enable HWAccel for FFmpeg decoder in Android? In other words, how use FFmpeg with his "mediacodec" module? (https://trac.ffmpeg.org/wiki/HWAccelIntro)

Samuel Audet

unread,
Nov 23, 2016, 9:23:59 AM11/23/16
to javacpp...@googlegroups.com
Why are you trying to get FFmpeg to use that when we can easily access
it from Java with the Android API??

Antoine LAVIER

unread,
Nov 23, 2016, 9:33:23 AM11/23/16
to javacpp
Just because with the Android MediaCodec API, some devices are able to support only
one MediaCodec instance at the time. And my app have to decode 1, 2, 3 or 4 simultaneous streams.
So I would like to test if the FFmpeg's mediacodec implementation have that limitation (and I think
it doesn't).

Antoine LAVIER

unread,
Nov 23, 2016, 10:29:20 AM11/23/16
to javacpp
Actually, maybe HWAccel is available on FFmpeg 3.2, in allcodecs.c is see:

REGISTER_HWACCEL(H264_MEDIACODEC, h264_mediacodec);

Have you plan to migrate FFmpeg in 3.2 or is it easy to try?

Samuel Audet

unread,
Nov 23, 2016, 9:08:24 PM11/23/16
to javacpp

Shouldn't be too hard to upgrade, yes. Your help would be welcome.


2016/11/24 0:29 "Antoine LAVIER" <antoine...@gmail.com>:
Actually, maybe HWAccel is available on FFmpeg 3.2, in allcodecs.c is see:

REGISTER_HWACCEL(H264_MEDIACODEC, h264_mediacodec);

Have you plan to migrate FFmpeg in 3.2 or is it easy to try?

--
You received this message because you are subscribed to the Google Groups "javacpp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javacpp-project+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Antoine LAVIER

unread,
Nov 24, 2016, 3:35:32 AM11/24/16
to javacpp
Ok, what should I do? I mean I never collaborate on an existing and advanced github project, what are the steps to follow (excluding dev part)?

Samuel Audet

unread,
Nov 24, 2016, 3:44:05 AM11/24/16
to javacpp

Well, try to update the build files to 3.2 and see what that gives. When you have questions or get stuck, posting here is fine.

Samuel Audet

unread,
Nov 26, 2016, 7:27:04 AM11/26/16
to javacpp, Antoine LAVIER
On 11/24/2016 05:44 PM, Samuel Audet wrote:
> Well, try to update the build files to 3.2 and see what that gives. When
> you have questions or get stuck, posting here is fine.

And then when you're done, or almost done, please send a pull request:
https://github.com/bytedeco/javacpp-presets/pulls
Thanks!

Samuel

Samuel Audet

unread,
Dec 2, 2016, 8:01:37 PM12/2/16
to Antoine LAVIER, javacpp
It didn't require too many changes, so I did here:
https://github.com/bytedeco/javacpp-presets/commit/bd106fafb98df8aeafbdb2bf1b341c82cd207bf8

Enjoy!

Samuel

On 11/26/2016 09:39 PM, Antoine LAVIER wrote:
> Ok! I'll start it soon.
Reply all
Reply to author
Forward
0 new messages