Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It may be bug in avformat.avio_open2 JNI wrapper #97

Closed
grimaz opened this issue Feb 26, 2015 · 3 comments
Closed

It may be bug in avformat.avio_open2 JNI wrapper #97

grimaz opened this issue Feb 26, 2015 · 3 comments
Labels

Comments

@grimaz
Copy link

grimaz commented Feb 26, 2015

Hello,
I can not find from which source the FFMPEG JNI came to your project, but it seems bug in
public static native int avio_open2(@ByPtrPtr avformat.AVIOContext var0, String var1, int var2, @const avformat.AVIOInterruptCB var3, @ByPtrPtr AVDictionary var4);
when call as
avformat.avio_open2(lDstContext.pb(), destination, avformat.AVIO_FLAG_WRITE, null, null);
APK crashes inside avio_open2/sub_... because storing value to null.

If you point me where you get JNI wrappers, I will check and send fix there.

Regards.

@grimaz
Copy link
Author

grimaz commented Feb 26, 2015

Seems it is not a bug, but @ByPtrPtr requires workaround.
Function call can not be used as
avformat.avio_open2(lDstContext.pb(), ...);
But only as
avformat.AVIOContext lAvioContext = new avformat.AVIOContext();
avformat.avio_open2(lAvioContext, ...);
lDstContext.pb(lAvioContext);

@saudet
Copy link
Member

saudet commented Feb 27, 2015

The JNI code is generated at build time, when executing mvn install or something. For that particular function we get this code:

JNIEXPORT jint JNICALL Java_org_bytedeco_javacpp_avformat_avio_1open2__Lorg_bytedeco_javacpp_avformat_00024AVIOContext_2Ljava_lang_String_2ILorg_bytedeco_javacpp_avformat_00024AVIOInterruptCB_2Lorg_bytedeco_javacpp_avutil_00024AVDictionary_2(JNIEnv* env, jclass cls, jobject arg0, jstring arg1, jint arg2, jobject arg3, jobject arg4) {
    ::AVIOContext* ptr0 = arg0 == NULL ? NULL : (::AVIOContext*)jlong_to_ptr(env->GetLongField(arg0, JavaCPP_addressFID));
    jint position0 = arg0 == NULL ? 0 : env->GetIntField(arg0, JavaCPP_positionFID);
    ptr0 += position0;
    const char* ptr1 = arg1 == NULL ? NULL : env->GetStringUTFChars(arg1, NULL);
    ::AVIOInterruptCB* ptr3 = arg3 == NULL ? NULL : (::AVIOInterruptCB*)jlong_to_ptr(env->GetLongField(arg3, JavaCPP_addressFID));
    jint position3 = arg3 == NULL ? 0 : env->GetIntField(arg3, JavaCPP_positionFID);
    ptr3 += position3;
    ::AVDictionary* ptr4 = arg4 == NULL ? NULL : (::AVDictionary*)jlong_to_ptr(env->GetLongField(arg4, JavaCPP_addressFID));
    jint rarg = 0;
    jthrowable exc = NULL;
    try {
        int rvalue = avio_open2((arg0 == NULL ? NULL : &ptr0), ptr1, arg2, (const ::AVIOInterruptCB*)ptr3, (arg4 == NULL ? NULL : &ptr4));
        rarg = (jint)rvalue;
    } catch (...) {
        exc = JavaCPP_handleException(env, 17);
    }

    ptr0 -= position0;
    if (arg0 != NULL) env->SetLongField(arg0, JavaCPP_addressFID, ptr_to_jlong(ptr0));
    if (arg1 != NULL) env->ReleaseStringUTFChars(arg1, ptr1);
    if (arg4 != NULL) env->SetLongField(arg4, JavaCPP_addressFID, ptr_to_jlong(ptr4));
    if (exc != NULL) {
        env->Throw(exc);
    }
    return rarg;
}

So, as you can see, when arg4 == NULL, it passes NULL to avio_open2(), and I'm afraid it looks like there is a bug in FFmpeg. Could you report that upstream and post a link here for reference? Thanks!

@saudet
Copy link
Member

saudet commented Feb 27, 2015

Let's see, I think you are asking about the first parameter? Yes, I see how this can be problematic... As you found out, and as stated in the README.md file, allocating a temporary output object is the way to go, but to prevent allocating native memory needlessly, we can call the constructor with a null value:

AVIOContext ioctx = new AVIOContext(null);
avformat.avio_open2(ioctx, ...);
fmtctx.pb(ioctx);

Unfortunately, I don't know of a nicer way to make this work, but if you have any ideas, please do share them! Thanks for reporting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants