1
votes

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!
    }
1
You might be interested in knowing that the H.264/AVC format that libx264 handles is patented, and you are expected to pay royalties to distribute a product based on it. As for your programming problem, have you tried other pixel formats, such as AV_PIX_FMT_YUV422P, or other values ?personne3000
I have tried other formats such as AV_PIX_FMT_YUV422P but the result is always the same.DundeeDave
So the exact same code works on Windows but fails on Android ? How did you get the two ffmpeg versions, did you compile both from source yourself ? ffmpeg needs to be compiled with x264 support (optional) to support it for example, so codecs support might be differentpersonne3000
I compiled them both from source myself yes. I hadn't looked at the licensing issues, that looks like it would be a problem. Can you suggest a different format?DundeeDave
Licensing might not be a problem if you are not selling a big app, but not being borderline on the legal side is always good. VP8 (with a webM container) can be a good alternative, if you know people will be able to read it. Google promotes VP8, so at least on recent versions of Android you should be fine (built in encoder on 4.3+, decoder on 2.3.3+)personne3000

1 Answers

0
votes

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.