[Libav-user] How to pass quality scale to mjpeg encoder

Tobias Rapp t.rapp at noa-archive.com
Fri Jan 26 11:17:09 EET 2018

On 25.01.2018 22:43, Carl Eugen Hoyos wrote:
> 2018-01-25 15:54 GMT+01:00 Tobias Rapp <t.rapp at noa-archive.com>:
>> On 25.01.2018 09:25, Tobias Rapp wrote:
>>> Hi,
>>> trying to do the equivalent of FFmpeg command-line output option "-q:v 3"
>>> on library level I came up with the following code (error checking stripped
>>> off):
>>>       AVCodecContext *enc_ctx = NULL;
>>>       const AVCodec *codec = avcodec_find_encoder_by_name("mjpeg");
>>>       int ret;
>>>       enc_ctx = avcodec_alloc_context3(codec);
>>>       enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
>>>       enc_ctx->global_quality = FF_QP2LAMBDA * 3.0;
>>>       ret = avcodec_open2(enc_ctx, enc_ctx->codec, NULL);
>>> Unfortunately it doesn't seem to have any effect on the output image size.
>>> Is there something obvious that I'm missing?
>> Answering myself: Apparently setting the encoder option is not sufficient
>> for mjpeg. The encoding quality has to be set for each frame that is passed
>> to the encoder.
>> Example:
>>      frame->quality = enc_ctx->global_quality;
>>      frame->pict_type = AV_PICTURE_TYPE_NONE;
>>      ret = avcodec_send_frame(enc_ctx, frame);
> Do you know if this was always needed?

I only tested the code with release/3.4 and current master, there it is 
necessary to set the quality on each frame. Don't know about older releases.

Also I don't know exactly the purpose behind it. Maybe some filters 
allow to adapt frame->quality based on scene-change, etc. But it would 
be great if the mjpeg encoder would detect an uninitialized 
frame->quality value and set it to enc_ctx->global_quality.


More information about the Libav-user mailing list