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

Improve Transcode Quality #832

Merged
merged 13 commits into from
Sep 18, 2023
Merged

Improve Transcode Quality #832

merged 13 commits into from
Sep 18, 2023

Conversation

leszko
Copy link
Contributor

@leszko leszko commented Aug 29, 2023

Phase 1 as described in the related Design Doc.

Related to livepeer/lpms#371 and livepeer/go-livepeer#2848

Before most Os update the go-livepeer version to support the CRF parameter, I suggest to do the following in order to improve slightly the transcode quality:

  • CRF=27
  • Bitrate=1.2*Current Bitrate

Rationale from Design Doc:

In Phase 1, we won’t really use the CRF parameter, so I’d set it to some conservative value that won’t make the transcoding quality worse (27 sounds like a good value) and still use bitrate to limit the max video bitrate. At the same time, I’d slightly increase the bitrate parameter, because we currently set it too low. For the highest resolution, we set it to the average bitrate of the source video, which results in a worse quality than the source, because we effectively set: max bitrate = source average bitrate. After this change, the quality will be slightly better and the segment size will be slightly bigger.

@@ -12,6 +12,7 @@ const (
MaxVideoBitrate = 288_000_000
TrackTypeVideo = "video"
TrackTypeAudio = "audio"
defaultCRF = 27
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 27? I think ffmpeg default is 23 actually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just from some tests. Please check this part of design doc for details. 23 will give way bigger segment size.

@leszko leszko marked this pull request as ready for review September 7, 2023 11:16
@leszko
Copy link
Contributor Author

leszko commented Sep 7, 2023

@mjh1 After my change, all the transcode profiles JSON tests are failing. Do you have any way to re-generate them or something? How did they got created?

// relativeBitrate needs to be slightly higher than the proportional average bitrate of the source video.
// Livepeer network uses bitrate to set max bitrate for encoding, so for the video to look good, we multiply
// it by a factor of 1.2.
relativeBitrate := 1.2 * float64(profile.Width*profile.Height) * (float64(videoBitrate) / float64(video.Width*video.Height))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did we determine the 1.2 multiplier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't really. I just know that what we currently have is too low, because we're setting: max bitrate = source avg bitrate which gives poor results.

As described in the doc, I plan to not set it at all, when the Os start to support the quality param (Phase 2).

profiles := make([]EncodedProfile, 0)
var CRF uint = defaultCRF
if videoProfile.CRF != 0 {
CRF = videoProfile.CRF
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRF = 0 is still a valid value (meaning lossless). Are we explicitly saying crf = 0 should never be allowed or should this check for values between 0 and 51?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point. The problem with 0 is that everywhere in go and protobuf, it's the default => so, the assumption is that 0 is a not set value.

Anyway, since as you know, there is no direct correspondence of the the quality value and the crf / cq, let's maybe not think about this corner case too much.

@leszko
Copy link
Contributor Author

leszko commented Sep 8, 2023

@emranemran I also renamed the crf to quality everywhere as we discussed. PTAL.

@codecov
Copy link

codecov bot commented Sep 8, 2023

Codecov Report

Merging #832 (e90142a) into main (cf5e881) will decrease coverage by 0.01061%.
The diff coverage is n/a.

Impacted file tree graph

@@                 Coverage Diff                 @@
##                main        #832         +/-   ##
===================================================
- Coverage   52.16754%   52.15693%   -0.01061%     
===================================================
  Files             63          63                 
  Lines           6136        6143          +7     
===================================================
+ Hits            3201        3204          +3     
- Misses          2675        2679          +4     
  Partials         260         260                 

see 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cf5e881...e90142a. Read the comment docs.

see 1 file with indirect coverage changes

@@ -134,7 +142,10 @@ func GetDefaultPlaybackProfiles(video InputTrack) ([]EncodedProfile, error) {
// check it here.
lowerQualityThanSrc := profile.Height < video.Height && profile.Bitrate < video.Bitrate
if lowerQualityThanSrc {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we also want this for the rendition that's the same resolution as the source?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that no, because for the same resolution as the source we create a profile in line 157. So, this logic here is (and always was) for the profile resolution lower than source.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh wait, maybe you're right that we should also increase the bitrate in line 157. Will update it there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 13a6bf2

@leszko leszko merged commit a03ce5d into main Sep 18, 2023
12 checks passed
@leszko leszko deleted the rafal/transcode-quality-params branch September 18, 2023 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants