Question

I am using FFMPEG + x264 on Android to encode YUV420 frames to a video file. I use the following code on each frame to encode them:

avcodec_encode_video2(gVideoWriteCodecCtx, &packet, pCurrentFrame, &gotPacket);

On the first few calls, the frame buffer gets filled and nothing is encoded. When the first encoding happens, a call is made to x264_lookahead_get_frames. I can see there that my frame array is correctly populated, but the first item is NULL. As a consequence, in x264_weights_analyse the reference frame gotten as frames[p0] is NULL and I get an exception there.

slicetype.c, the first frame in "frames" is NULL

if( h->param.analyse.i_weighted_pred && b == p1 )
    {
        x264_emms();
        x264_weights_analyse( h, fenc, frames[p0], 1 );
        w = fenc->weight[0];
    }

And the exception happens there, ref is NULL

static void x264_weights_analyse( x264_t *h, x264_frame_t *fenc, x264_frame_t *ref, int b_lookahead )
    {
        int i_delta_index = fenc->i_frame - ref->i_frame - 1;

I surely am missing something as I am sure this encoder works for most people :) Does anyone have an idea why this first frame in the "frames" array is null?

Many thanks

Was it helpful?

Solution

If that can help anyone, it looks like something weird in the library. Inverting these two lines in lookahead.c line236 made the job:

x264_stack_align( x264_slicetype_decide, h );
x264_lookahead_update_last_nonb( h, h->lookahead->next.list[0] );

to

x264_lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
x264_stack_align( x264_slicetype_decide, h );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top