简体   繁体   中英

Is there a faster way to grab frames by skipping frames?

Im looping through av_read_frame and avcodec_decode_video2 to read pixel data from an mp4 video.

int i = 0;
while (av_read_frame(pFormatCtx, &pkt)>=0) {
    AVPacket orig_pkt = pkt;
    do {
        int ret = 0;
        int decoded = pkt.size;
        got
        if (i%7==0) { // Only process 1/7th of frames
            ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &pkt);
            if (got_frame) {
                // get pixel data
            } else {
                printf("Error: could not get frame %d", i );
            }
        }
        ret = decoded;
        pkt.data += ret;
        pkt.size -= ret;
    } while (pkt.size > 0);
    av_free_packet(&orig_pkt);
}

It's a 35FPS video, but I only need to read about 5FPS of pixel data.

The i%7==0 conditional results in more than 1/7th of the frames being dropped. Removing that line results in all frames being slowly processed with no frames dropped.

Is there a fast way to only read 1/7th of the frames?

It depends on the video. Most video are compressed into a "Group of pictures" Where the first picture is a keyframe, and the rest are predicted frames. A key frame can be decode by itself, but a predicted frame can only be decoded if all previous predicted frames up to and including the previous key frame was decode (Its actually a little more complicated than that, but good enough for now). If the video you are decoding is all key frames (MJPEG for example), Yes, you can skip whatever frames you want. If its not, and you skip a frame, you must skip all frames up to the next keyframe. Keyframes are usually about 2 to 10 second appart (60-300 frames assuming 30fps video). But it could be more or less.

You can check if a frame is a keyframe by checking for AV_PKT_FLAG_KEY & pkt.flags

You could do:

if (AV_PKT_FLAG_KEY &pkt.flags) {
    ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &pkt);
...
}

But then you don't get to decide what frames you decode, the video file itself does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM