Hello all,
I have a C++ function that is supposed to take in a Frame and save it to MJPEG
with a desired quality.
After calling avcodec_open2(pJpgCodecCtx, pJpgCodec, NULL), I try to set the
quality using the following code:
pJpgCodecCtx->flags |= AV_CODEC_FLAG_QSCALE;
pJpgCodecCtx->global_quality = FF_QUALITY_SCALE * quality;
But changing the quality parameter value doesn’t affect quality of the final
encoded MJPEG file.
Am I doing this the right way ? Or are there other fields used to compute
quality ?
Here is the whole code for the function:
void writeJPGFile(AVCodec *pJpgCodec, AVCodecContext *pJpgCodecCtx, AVFrame
*pFrame, int number, std::string outDir, int quality) {
pJpgCodecCtx->time_base = {.num=1, .den=1};
pJpgCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;
pJpgCodecCtx->height = pFrame->height;
pJpgCodecCtx->width = pFrame->width;
// Init jpeg codec context using codec params
if (avcodec_open2(pJpgCodecCtx, pJpgCodec, NULL) < 0) {
return;
}
pJpgCodecCtx->flags |= AV_CODEC_FLAG_QSCALE;
pJpgCodecCtx->global_quality = FF_QUALITY_SCALE * quality;
FILE *pFile;
char filename[256];
AVPacket *pPacket = av_packet_alloc();
av_init_packet(pPacket);
// Send frame to encoder
if (avcodec_send_frame(pJpgCodecCtx, pFrame) < 0) {
return;
}
// Open the file to be written as writable & binary
sprintf(filename, &outDir[0], number);
pFile = fopen(filename, "wb");
if (pFile == NULL) {
return;
}
int ret = 0;
while (ret >= 0) {
ret = avcodec_receive_packet(pJpgCodecCtx, pPacket);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
break;
}
fwrite(pPacket->data, 1, static_cast<size_t>(pPacket->size), pFile);
av_packet_unref(pPacket);
}
fclose(pFile);
avcodec_close(pJpgCodecCtx);
}
Best regards,
Corentin Bruneau.
_______________________________________________
libav-api mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-api