Skip to content

Commit

Permalink
Merge pull request #477 from jasaw/tune-h264-encode-quality
Browse files Browse the repository at this point in the history
Tune h264_omx encode video quality
  • Loading branch information
Mr-Dave authored Sep 17, 2017
2 parents 8863857 + 86166cf commit 924ec9f
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions ffmpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,21 +466,30 @@ static int ffmpeg_set_pts(struct ffmpeg *ffmpeg, const struct timeval *tv1){

static int ffmpeg_set_quality(struct ffmpeg *ffmpeg){

char crf[4];

ffmpeg->opts = 0;
if (ffmpeg->vbr > 100) ffmpeg->vbr = 100;
if (ffmpeg->ctx_codec->codec_id == MY_CODEC_ID_H264 ||
ffmpeg->ctx_codec->codec_id == MY_CODEC_ID_HEVC){
if (ffmpeg->vbr > 0) {
ffmpeg->vbr = (int)(( (100-ffmpeg->vbr) * 51)/100);
if (ffmpeg->vbr <= 0)
ffmpeg->vbr = 45; // default to 45% quality
av_dict_set(&ffmpeg->opts, "preset", "ultrafast", 0);
av_dict_set(&ffmpeg->opts, "tune", "zerolatency", 0);
if ((strcmp(ffmpeg->codec->name, "h264_omx") == 0) || (strcmp(ffmpeg->codec->name, "mpeg4_omx") == 0)) {
// H264 OMX encoder quality can only be controlled via bit_rate
// bit_rate = ffmpeg->width * ffmpeg->height * ffmpeg->fps * quality_factor
ffmpeg->vbr = (ffmpeg->width * ffmpeg->height * ffmpeg->fps * ffmpeg->vbr) >> 7;
// Clip bit rate to min
if (ffmpeg->vbr < 4000) // magic number
ffmpeg->vbr = 4000;
ffmpeg->ctx_codec->profile = FF_PROFILE_H264_HIGH;
ffmpeg->ctx_codec->bit_rate = ffmpeg->vbr;
} else {
ffmpeg->vbr = 28;
// Control other H264 encoders quality via CRF
char crf[4];
ffmpeg->vbr = (int)(( (100-ffmpeg->vbr) * 51)/100);
snprintf(crf, 4, "%d", ffmpeg->vbr);
av_dict_set(&ffmpeg->opts, "crf", crf, 0);
}
snprintf(crf, 4, "%d",ffmpeg->vbr);
av_dict_set(&ffmpeg->opts, "preset", "ultrafast", 0);
av_dict_set(&ffmpeg->opts, "tune", "zerolatency", 0);
av_dict_set(&ffmpeg->opts, "crf", crf, 0);
} else {
/* The selection of 8000 in the else is a subjective number based upon viewing output files */
if (ffmpeg->vbr > 0){
Expand All @@ -489,7 +498,7 @@ static int ffmpeg_set_quality(struct ffmpeg *ffmpeg){
ffmpeg->ctx_codec->global_quality=ffmpeg->vbr;
}
}
MOTION_LOG(INF, TYPE_ENCODER, NO_ERRNO, "vbr/crf for codec: %d", ffmpeg->vbr);
MOTION_LOG(INF, TYPE_ENCODER, NO_ERRNO, "%s codec vbr/crf/bit_rate: %d", ffmpeg->codec->name, ffmpeg->vbr);

return 0;
}
Expand Down

0 comments on commit 924ec9f

Please sign in to comment.