Search code examples
c++ffmpeglibavcodeclibavformat

Set "re" flag in libavformat


how can we set re flag in c++ code which uses libavformat and libavcodec libraries. I need to implement something like following command in my c++ application

ffmpeg -re -f m4v -i video.264 -vcodec copy out.mp4

i have implemented and tested the above command but without re flag. I need my application to read frames at the same rate at whihc they are encoded.


Solution

  • "re" flag sets InputFile->rate_emu flag . Its occurrence can be seen in ffmpeg_opt.c.

    InputFile is a local struct to ffmpeg.h which indicates that "re" flag has no use in either libavcodec or libavformat.

    As per how to use that functionality, if you look into ffmpeg_opt.c

     { "re",             OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
                        OPT_INPUT,                                   { .off = OFFSET(rate_emu) },
        "read input at native frame rate", "" },
    

    and then look into

    ffmpeg.c

    static int get_input_packet(InputFile *f, AVPacket *pkt)
    {
        if (f->rate_emu) {
            int i;
            for (i = 0; i < f->nb_streams; i++) {
                InputStream *ist = input_streams[f->ist_index + i];
                int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
                int64_t now = av_gettime_relative() - ist->start;
                if (pts > now)
                    return AVERROR(EAGAIN);
            }
        }
    
    #if HAVE_PTHREADS
        if (nb_input_files > 1)
            return get_input_packet_mt(f, pkt);
    #endif
        return av_read_frame(f->ctx, pkt);
    }
    

    If the "rate_emu" flag is set, the get_input_packet rescales the pts and checks if its time to read the frame . If the time is right, it reads the frame else, it returns empty handed. so it feels like we are getting it at native framerate. if rate_emu is not set, the code jumps directly to "av_read_frame