FFmpeg - avcodec_receive_frame returns 0 but frames are invalid

1

I've been trying to extract images from videos, but not those with PNG codec. My code works fine with those with JPEG. avcodec_receive_frame worked successfully but the data of the frames was like trash? Do I have to do something special to demuxing when dealing with PNG?

Exception thrown at 0x00007FFF7DF34B9A (msvcrt.dll) in Program.exe: 0xC0000005: Access violation reading location 0x00000000000003F0 when calling avcodec_send_frame in my saveImage function, which means I was accessing invalid or unalloacted memory I guess. How this happened?

enter image description here

Just suppose all the function calls returned 0 until exception thrown.

Decoding:

bool ImageExtractor::decode() {
    // some other code here
    ret = avcodec_send_packet(codec_ctx, packet); // returned 0
    ret = avcodec_receive_frame(codec_ctx, frame); // returned 0
    if (ret == 0) {
        if (count >= target_frame) {
            snprintf(buf, sizeof(buf), "%s/%d.png", destination.toLocal8Bit().data(), count);
            saveImage(frame, buf); // a function that writes images on disk 
       }
    // some other code here
}


bool ImageExtractor::saveImage(AVFrame *frame, char *destination) {
     AVFormatContext *imgFmtCtx = avformat_alloc_context();
     pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);

     // some other code here

    if (!frame)
        std::cout << "invalid frame \n";

    if (!imgCodecCtx) // AV_CODEC_ID_MJPEG(7)
        std::cout << "invalid codec ctx \n";

    ret = avcodec_send_frame(imgCodecCtx, frame); // everything stopped here
}

Demuxing:

avformat_open_input(&format_ctx, source.toLocal8Bit(), nullptr, nullptr);
vsi = av_find_best_stream(format_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
codec_par = avcodec_parameters_alloc();
avcodec_parameters_copy(codec_par, format_ctx->streams[vsi]->codecpar);

AVCodec* codec = avcodec_find_decoder(codec_par->codec_id); // AV_CODEC_ID_PNG(61)
codec_ctx = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(codec_ctx, codec_par);
avcodec_parameters_free(&codec_par);
avcodec_open2(codec_ctx, codec, 0);
ffmpeg
decode
asked on Stack Overflow Jul 9, 2019 by Jinx

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0