1
votes

I'm trying ton encode video from set of jpeg images to h264, using ffmpeg + x264 for it. I init AVCodecContext in such way:

_outputCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
_outputCodecContext = avcodec_alloc_context3(_outputCodec);
avcodec_get_context_defaults3(_outputCodecContext, _outputCodec);
_outputCodecContext->width                 = _currentWidth;
_outputCodecContext->height                = _currentHeight;
_outputCodecContext->pix_fmt               = AV_PIX_FMT_YUV420P;
_outputCodecContext->time_base.num         = 1;
_outputCodecContext->time_base.den         = 25;
_outputCodecContext->profile =FF_PROFILE_H264_BASELINE;
_outputCodecContext->level = 50;

avcodec_open return no errors, anything is OK, but when I call avcodec_encode_video2() I get such messages (I think it's from x264):

using mv_range_thread = %d

%s

profile %s, level %s

And then app crashs. My be there are more neccessary settings for codec context, when use x264 &&

1
Perhaps the problem is the mismatch pixel format (for example, source have RGB24 format, encoder requires YUV420 format). Maybe you'll have to convert the pixel format using swscale library - pogorskiy

1 Answers

1
votes

Without a full version of your code it is hard to see what the actual problem is.

Firstly here is a working example of the FFMPEG library encoding RGB frames to a H264 video:

http://www.imc-store.com.au/Articles.asp?ID=276

You could expand on this example by using CImage to load in your JPGs and pass the RGB data to the FFMPEG Class to encode to a H264 video.

A few thoughts on your example though:

  • Have you called register all like below?

    avcodec_register_all();
    av_register_all();
    
  • Also I'd re-write your code to be something like below:

    AVStream *st;
    m_video_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
    st = avformat_new_stream(_outputCodec, m_video_codec);
    _outputCodecContext = st->codec;
    _outputCodecContext->codec_id = m_fmt->video_codec;
    _outputCodecContext->bit_rate = m_AVIMOV_BPS;           //Bits Per Second 
    _outputCodecContext->width    = m_AVIMOV_WIDTH;         //Note Resolution must be a multiple of 2!!
    _outputCodecContext->height   = m_AVIMOV_HEIGHT;        //Note Resolution must be a multiple of 2!!
    _outputCodecContext->time_base.den = m_AVIMOV_FPS;      //Frames per second
    _outputCodecContext->time_base.num = 1;
    _outputCodecContext->gop_size      = m_AVIMOV_GOB;      // Intra frames per x P frames
    _outputCodecContext->pix_fmt       = AV_PIX_FMT_YUV420P;//Do not change this, H264 needs YUV format not RGB
    
  • And then you need to convert the RGB JPG picture to the YUV format using swscale as pogorskiy said.

Have a look at the linked example, I tested it on VC++2010 and it works perfectly and you can send it an RGB char array.