Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMPEG sws_scale Crash on Android

I have an app that convert images to video, in Google Play I see the following crash (which the only details I get is the name of the function and I don't understand the rest):

backtrace:
#00 pc 0000cc78 /data/app-lib/com.myapp-1/libswscale.so (sws_scale+204)
#01 pc 000012af /data/app-lib/com.myapp-1/libffmpeg.so (OpenImage+322)

code around pc:
79065c58 e58d8068 e58d2070 e58d3074 059d00b0 

The code point to the function sws_scale, the code works almost all the time on my device (Nexus 5) but I see a lot of reports even with the same device with that issue. Any idea why this could happen?

AVFrame* OpenImage(const char* imageFileName, int W_VIDEO, int H_VIDEO, int* numBytes)
{
    AVFormatContext *pFormatCtx;
    AVCodecContext *pCodecCtx;
    AVCodec *pCodec;
    AVFrame *pFrame;
    int frameFinished;
    uint8_t *buffer;
    AVPacket packet;
    int srcBytes;

    AVFrame* frame2 = NULL;// scaled frame
    uint8_t* frame2_buffer;
    struct SwsContext *resize;

    if(av_open_input_file(&pFormatCtx, imageFileName, NULL, 0, NULL)!=0)
    {
        LOGI("Can't open image file '%s'\n", imageFileName);
        return NULL;
    }
    //dump_format(pFormatCtx, 0, imageFileName, 0);
    if (av_find_stream_info(pFormatCtx) < 0)
    {
        LOGI("Can't find stream info.");
        return NULL;
    }
    pCodecCtx = pFormatCtx->streams[0]->codec;
    pCodecCtx->pix_fmt = PIX_FMT_YUV420P;

    // Find the decoder for the video stream
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (!pCodec)
    {
        LOGI("Codec not found\n");
        return NULL;
    }

    // Open codec
    if(avcodec_open(pCodecCtx, pCodec)<0)
    {
        LOGI("Could not open codec\n");
        return NULL;
    }
    pFrame = avcodec_alloc_frame();
    if (!pFrame)
    {
        LOGI("Can't allocate memory for AVFrame\n");
        return NULL;
    }

    // Determine required buffer size and allocate buffer
    srcBytes = avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
    buffer = (uint8_t *) av_malloc(srcBytes * sizeof(uint8_t));
    avpicture_fill((AVPicture *) pFrame, buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);

    // Read frame
    if (av_read_frame(pFormatCtx, &packet) >= 0)
    {
        int ret;
//      if(packet.stream_index != 0)
//          continue;
        ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
        if (ret > 0)
        {
            //LOGI("Frame is decoded, size %d\n", ret);
            pFrame->quality = 4;

            // Create another frame for resized result
            frame2 = avcodec_alloc_frame();
            *numBytes = avpicture_get_size(PIX_FMT_YUV420P, W_VIDEO, H_VIDEO);
            frame2_buffer = (uint8_t *)av_malloc(*numBytes * sizeof(uint8_t));
            avpicture_fill((AVPicture*)frame2, frame2_buffer, PIX_FMT_YUV420P, W_VIDEO, H_VIDEO);

            // Get resize context
            resize = sws_getContext(pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, W_VIDEO, H_VIDEO, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);

            // frame2 should be filled with resized samples
            ret = sws_scale(resize, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, frame2->data, frame2->linesize);
            sws_freeContext(resize);
        }
        else
            LOGI("Error [%d] while decoding frame: %s\n", ret, strerror(AVERROR(ret)));
    }
    av_free(pFrame);
    av_free_packet(&packet);
    avcodec_close(pCodecCtx);
    //av_free(pCodecCtx);
    av_close_input_file(pFormatCtx);
    return frame2;
}
like image 940
Jimmy Avatar asked Sep 30 '22 21:09

Jimmy


1 Answers

After your avcodec_decode_video2, do not check ret only. You need to check frameFinished too. if frameFinished == 0, your frame should not be used (because not filled). I do not know for images, but when you decode video, it happens very often. You need to read the next packet and give to the next call of avcodec_decode_video2.

On a side note: why are you forcing pCodecCtx->pix_fmt = PIX_FMT_YUV420P? It is automatically set to the correct format by av_find_stream_info, and you should use it as sws_getContext parameter.

Last thing: no need to fill your pFramewith avpicture_fill. You only need to av_frame_alloc() it, and avcodec_decode_video2 will take care of filling it.

like image 57
biskitt Avatar answered Oct 04 '22 03:10

biskitt