Search code examples
androidandroid-ndkffmpegx264libx264

FFMPEG x264 encoder Android


I've compiled an FFMPEG library for use on Android with libx264 and using the NDK.

I want to encode an MPEG video file however the application is failing when opening the encoder codec, in avcodec_open2.

The FFMPEG logs I receive from avcodec_open2 are below with the function returning -22.

  • Picture size %ux%u is invalid.
  • ignoring invalid width/height values
  • Specified pix_fmt is not supported

On windows this code works fine, it's only on Android that there is a failure. Any ides why this would fail on Android?

if (!(codec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO)))
    {
        return -1;
    }

    //Allocate context based on codec
    if (!(context = avcodec_alloc_context3(codec)))
    {
        return -2;
    }

    //Setup Context
    // put sample parameters
    context->bit_rate = 4000000;
    // resolution must be a multiple of two
    context->width = 1280;
    context->height = 720;
    // frames per second
    context->time_base = (AVRational){1,25};
    context->inter_quant_bias = 96;
    context->gop_size = 10;
    context->max_b_frames = 1;
    //IDs
    context->pix_fmt = AV_PIX_FMT_YUV420P;
    context->codec_id = AV_CODEC_ID_MPEG1VIDEO;
    context->codec_type = AVMEDIA_TYPE_VIDEO;


    if (AV_CODEC_ID_MPEG1VIDEO == AV_CODEC_ID_H264)
    {
        av_opt_set(context->priv_data, "preset", "slow", 0);
    }

    if ((result = avcodec_open2(context, codec, NULL)) < 0)
    {
        //Failed opening Codec!
    }

Solution

  • This problem was caused by building FFMPEG with outdated source code.

    I got the most recent source from https://www.ffmpeg.org/ and compiled it in the same way and the new library works fine.

    Note: I hadn't considered the full implications regarding licenses of using libx264. I've since dropped it.