Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

motion: reworked movie encode quality #1105

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions package/motion/tune-h264-encode-quality.patch
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
diff --git a/ffmpeg.c b/ffmpeg.c
index a4d3757..368c855 100644
index 87b4f75..4816adf 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -451,7 +451,7 @@ static int ffmpeg_set_pts(struct ffmpeg *ffmpeg, const struct timeval *tv1){
@@ -451,21 +451,30 @@ static int ffmpeg_set_pts(struct ffmpeg *ffmpeg, const struct timeval *tv1){

static int ffmpeg_set_quality(struct ffmpeg *ffmpeg){

- char crf[4];
+ int bit_rate;

-
ffmpeg->opts = 0;
if (ffmpeg->vbr > 100) ffmpeg->vbr = 100;
@@ -459,13 +459,24 @@ static int ffmpeg_set_quality(struct ffmpeg *ffmpeg){
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);
+ //bit_rate = ffmpeg->width * ffmpeg->height * ffmpeg->fps * quality_factor
+ bit_rate = (ffmpeg->width * ffmpeg->height * ffmpeg->fps * ffmpeg->vbr) >> 8;
- 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 {
+ bit_rate = ffmpeg->bps;
ffmpeg->vbr = 28;
- 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);
+ av_dict_set(&ffmpeg->opts, "preset", "ultrafast", 0);
+ av_dict_set(&ffmpeg->opts, "tune", "zerolatency", 0);
+ av_dict_set_int(&ffmpeg->opts, "crf", ffmpeg->vbr, 0);
+ if ((strcmp(ffmpeg->codec->name, "h264_omx") == 0) || (strcmp(ffmpeg->codec->name, "mpeg4_omx") == 0)) {
+ // Clip bit rate to min and max
+ if (bit_rate < 40000)
+ bit_rate = 40000;
+ else if (bit_rate > 3000000)
+ bit_rate = 3000000;
+ ffmpeg->ctx_codec->profile = FF_PROFILE_H264_HIGH;
+ ffmpeg->ctx_codec->bit_rate = bit_rate;
+ }
} else {
/* The selection of 8000 in the else is a subjective number based upon viewing output files */
if (ffmpeg->vbr > 0){
@@ -474,7 +483,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;
}