I'm using libavcodec, version 9.7, to write a simple demo, almost exactly like example in official example.
However, I can't open encoder. Also, av_opt_set(context->priv_data, "preset", "slow", 0)
always leads to crush.
This is my code:
// other code...
int ret = 0;
avcodec_register_all();
AVCodec* codec = NULL;
AVCodecContext* context = NULL;
AVFrame* frame = NULL;
uint8_t endcode[] = { 0, 0, 1, 0xb7 };
codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if(!codec){
qDebug()<<"cannot find encoder";
return;
}
qDebug()<<"encoder found";
context = avcodec_alloc_context3(codec);
if(!context){
qDebug()<<"cannot alloc context";
return;
}
qDebug()<<"context allocted";
context->bit_rate = 400000;
/* resolution must be a multiple of two */
context->width = 352;
context->height = 288;
/* frames per second */
context->time_base= (AVRational){1,25};
context->gop_size = 10; /* emit one intra frame every ten frames */
context->max_b_frames=1;
context->pix_fmt = AV_PIX_FMT_YUV420P;
qDebug()<<"context init";
// av_opt_set(context->priv_data, "preset", "slow", 0); // this will crush
AVDictionary *d = NULL;
av_dict_set(&d, "preset", "ultrafast",0); // this won't
ret = avcodec_open2(context, codec, &d);
if ( ret < 0) {
qDebug()<<"cannot open codec"<<ret;
return;
}
qDebug()<<"codec open";
// other code...
This outputs:
encoder found
context allocted
context init
cannot open codec -22
[libx264 @ 0340B340] [IMGUTILS @ 0028FC34] Picture size 0x10 is invalid
[libx264 @ 0340B340] ignoring invalid width/height values
[libx264 @ 0340B340] Specified pix_fmt is not supported
I don't think the width/height is invalid and format there either. I have no idea what's wrong here.
Any help. plz?
This is an issue of libav, I haven't checked its issue list though. The code runs great when I use another daily build, 20131101.