0

我有一个正常工作的本机 C 函数,我从我的 java 代码中调用它。但是,当我将此代码拆分为两个函数并依次调用它们时,我得到了致命错误。

//global variables
AVFormatContext *pFormatCtx;
AVFrame         *pFrame;
AVFrame         *pFrameRGB;
AVCodecContext  *pCodecCtx;
AVCodec         *pCodec;
uint8_t         *buffer;
int             videoStream;
struct SwsContext      *sws_ctx = NULL;
int outWidth, outHeight;

工作未拆分功能

JNIEXPORT void JNICALL Java_foo(JNIEnv * env, jclass class) {
    av_register_all();
    const char* videoPath = "11.mp4";

    int             numBytes;
    AVDictionary    *optionsDict = NULL;

    pFrame = NULL; 
    pFrameRGB = NULL;
    buffer = NULL;
    pCodec = NULL;
    pFormatCtx = NULL;

    // Open video file
    if(avformat_open_input(&pFormatCtx, videoPath, NULL, NULL)!=0)
            exit(1); // Couldn't open file


    // Retrieve stream information
    if(avformat_find_stream_info(pFormatCtx, NULL)<0)
            exit(1); // Couldn't find stream information

     av_dump_format(pFormatCtx, 0,videoPath, 0);


    // Find the first video stream
    videoStream=-1;
    int i;
    for(i=0; i<pFormatCtx->nb_streams; i++) {
            if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
                    videoStream=i;
                    break;
            }
    }

    if(videoStream==-1)
            exit(1); // Didn't find a video stream

    // Get a pointer to the codec context for the video stream
    pCodecCtx=pFormatCtx->streams[videoStream]->codec;


    // Find the decoder for the video stream
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL) {
            fprintf(stderr, "Unsupported codec!\n");
            exit(1); // Codec not found
    }

    // Open codec
    if(avcodec_open2(pCodecCtx, pCodec, &optionsDict)<0)
            exit(1); // Could not open codec

    // Allocate video frame
    pFrame=avcodec_alloc_frame();

    // Allocate an AVFrame structure
    pFrameRGB=avcodec_alloc_frame();
    if(pFrameRGB==NULL)
            exit(1);

    outWidth = 128;
    outHeight = 128;

    // Determine required buffer size and allocate buffer
    numBytes=avpicture_get_size(PIX_FMT_RGB24, outWidth, outHeight);
    buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

    sws_ctx = sws_getContext(
                            pCodecCtx->width,
                            pCodecCtx->height,
                            pCodecCtx->pix_fmt,
                            outWidth,
                            outHeight,
                            PIX_FMT_RGB24,
                            SWS_BILINEAR,
                            NULL,
                            NULL,
                            NULL
                    );

    // Assign appropriate parts of buffer to image planes in pFrameRGB
    // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
    // of AVPicture
    avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, outWidth, outHeight);
}

拆分功能失败

JNIEXPORT void JNICALL Java_foo1(JNIEnv * env, jclass class) {
    av_register_all();
}

JNIEXPORT void JNICALL Java_foo2(JNIEnv * env, jclass class) {
    //all lines of code from Java_foo exept the first
}

Java 代码

System.loadLibrary("mylib");
Mylib.foo1();
Mylib.foo2(); //fatal error


#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007faab5012dc0, pid=15571, tid=140371352766208

有任何想法吗?

4

0 回答 0