[x264-devel] encode x264 video with zero delay

"Matthias Bühlmann" genesys at gmx.ch
Sun Apr 15 21:34:47 CEST 2012


Hello there

I use FFMPEG in my application and try to encode a video with 0 delay (i.e. i need the decoded frame already before encoding the next one). I found this question from this mailing list: http://mailman.videolan.org/pipermail/x264-devel/2009-May/005880.html

Apparently he was able to get this to work, but for me avcodec_encode_video() returns data only on the (gop_size+2)st frame. so - if I set gop_size to 12, the function returns > 0 on the 14th frame. If I set it to 0 i then it will return > 0 on the 2nd frame. But i want it to return > 0 on the 1st frame already. This is how I setup the codecContext:

static AVStream* add_video_stream(AVFormatContext *oc, enum CodecID codec_id, int w, int h, int fps)
{
    AVCodecContext *c;
    AVStream *st;
    AVCodec *codec;

    /* find the video encoder */
    codec = avcodec_find_encoder(codec_id); //CODEC_ID_H264
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    st = avformat_new_stream(oc, codec);
    if (!st) {
        fprintf(stderr, "Could not alloc stream\n");
        exit(1);
    }

    c = st->codec;

    /* Put sample parameters. */
    c->bit_rate = 400000;
    /* Resolution must be a multiple of two. */
    c->width    = w;
    c->height   = h;
    /* timebase: This is the fundamental unit of time (in seconds) in terms
     * of which frame timestamps are represented. For fixed-fps content,
     * timebase should be 1/framerate and timestamp increments should be
     * identical to 1. */
    c->time_base.den = fps;
    c->time_base.num = 1;
    c->gop_size      = 12; /* emit one intra frame every twelve frames at most */
	
	//Testing:
	c->codec = codec; //need this for avcodec_open2()
	c->codec_type = AVMEDIA_TYPE_VIDEO;
	c->coder_type = FF_CODER_TYPE_VLC;
	c->me_method = 7; //motion estimation algorithm
	c->me_subpel_quality = 4;
	//c->delay = 0; //doesn't seem to do anything
	c->max_b_frames = 0; //no b frames
	c->thread_count = 1; // more than one threads increase delay -must be 1 for sure to achieve 0 delay (0 will create numCPU+1 threads)
	c->refs = 3; //number of reference frames, doesn't seem to affect delay
	c->flags |= CODEC_FLAG_LOW_DELAY; // doesn't seem to have any influence

	//c->gop_size      = 0;
    
	c->pix_fmt       = STREAM_PIX_FMT;

    /* Some formats want stream headers to be separate. */
    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;

    return st;
}



Can anyone point me to how to setup the codercontext correctly to get 0 delay encoding with x264?
-- 
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!                                  
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a


More information about the x264-devel mailing list