2
votes

I'm trying to implement a function which can get the next frame, but when i call this function at the 3rd time

int ret = decoder.get_next_frame(mat);
ret = decoder.get_next_frame(mat);
ret = decoder.get_next_frame(mat); // error 

ffmpeg avcodec_send_packet return err, and the msg is [h264 @ 0x8bd2780] Invalid NAL unit size (10848 > 766). [h264 @ 0x8bd2780] Error splitting the input into NAL units. the var with prefix '_' are all member var

int VideoDecoder::get_next_frame(cv::Mat& mat) {
int ret = 0;
bool got_frame = false;

while (!got_frame && av_read_frame(_p_fmt_ctx, _p_packet) >= 0) {
    if (_p_packet->stream_index == _video_stream_index) {

        ret = avcodec_send_packet(_p_dec_ctx, _p_packet);

        if (ret < 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
            break;
        }   

        while (ret >= 0) {
            ret = avcodec_receive_frame(_p_dec_ctx, _p_frame);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                break;
            }   

            sws_scale(_p_img_convert_ctx, _p_frame->data, _p_frame->linesize,
                    0, _p_dec_ctx->height, _p_frame_rgb->data, _p_frame_rgb->linesize);
            mat = cv::Mat(_p_frame->height, _p_frame->width, CV_8UC3, _p_frame_rgb->data[0]);
            got_frame = true;
        }   
    }   
    av_packet_unref(_p_packet);
}   

if (got_frame) {
    return _p_dec_ctx->frame_number;
}   

return 0;
}

so what's the problem?

1
Hello to stack overflow (SO). We here at SO are eager to help you, but please play along our rules, to make it easier for us. Consider reading through the Help and taking the Tour. Especially consider reading through How do I ask a good question? and How to create a Minimal, Complete, and Verifiable example. - skratchi.at
Could you try with another video just to be sure the problem isn't with the file being decoded? - Leo Adberg
the video file is ok, because if i change the code from "while (!got_frame && av_read_frame(_p_fmt_ctx, _p_packet) >= 0)" to "while ( av_read_frame(_p_fmt_ctx, _p_packet) >= 0)" all the frame can be cv::imwrite correctly - protoss
Please do not spam unrelated languages. You yourself know that you're writing C++ here. - Antti Haapala -- Слава Україні
useless code removed - protoss

1 Answers

0
votes

I re-check the code, finally found a memory err, the video data released unexpected in other place, so the above codes seems ok