Skip to content

Commit

Permalink
Add 1.2 multiplier also for the source video bitrate
Browse files Browse the repository at this point in the history
  • Loading branch information
leszko committed Sep 12, 2023
1 parent 4a96b00 commit 13a6bf2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 5 additions & 4 deletions video/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const (
TrackTypeVideo = "video"
TrackTypeAudio = "audio"
defaultQuality = 27
// 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.
maxBitrateFactor = 1.2
)

type InputVideo struct {
Expand Down Expand Up @@ -143,9 +146,7 @@ func GetDefaultPlaybackProfiles(video InputTrack) ([]EncodedProfile, error) {
lowerQualityThanSrc := profile.Height < video.Height && profile.Bitrate < video.Bitrate
if lowerQualityThanSrc {
// 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))
relativeBitrate := maxBitrateFactor * float64(profile.Width*profile.Height) * (float64(videoBitrate) / float64(video.Width*video.Height))
br := math.Min(relativeBitrate, float64(profile.Bitrate))
profile.Bitrate = int64(br)
profiles = append(profiles, profile)
Expand All @@ -156,7 +157,7 @@ func GetDefaultPlaybackProfiles(video InputTrack) ([]EncodedProfile, error) {
}
profiles = append(profiles, EncodedProfile{
Name: strconv.FormatInt(nearestEven(video.Height), 10) + "p0",
Bitrate: videoBitrate,
Bitrate: int64(math.Min(maxBitrateFactor*float64(videoBitrate), MaxVideoBitrate)),
FPS: 0,
Width: nearestEven(video.Width),
Height: nearestEven(video.Height),
Expand Down
16 changes: 8 additions & 8 deletions video/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) {
},
want: []EncodedProfile{
{Name: "low-bitrate", Width: 640, Height: 360, Bitrate: 500_000, Quality: defaultQuality},
{Name: "360p0", Width: 640, Height: 360, Bitrate: 1_000_001, Quality: defaultQuality},
{Name: "360p0", Width: 640, Height: 360, Bitrate: 1_200_001, Quality: defaultQuality},
},
},
{
Expand All @@ -42,7 +42,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) {
},
want: []EncodedProfile{
{Name: "low-bitrate", Width: 640, Height: 360, Bitrate: 250_000, Quality: defaultQuality},
{Name: "360p0", Width: 640, Height: 360, Bitrate: 500_000, Quality: defaultQuality},
{Name: "360p0", Width: 640, Height: 360, Bitrate: 600_000, Quality: defaultQuality},
},
},
{
Expand All @@ -57,7 +57,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) {
},
want: []EncodedProfile{
{Name: "360p0", Width: 640, Height: 360, Bitrate: 1_000_000, Quality: defaultQuality},
{Name: "720p0", Width: 1280, Height: 720, Bitrate: 4_000_001, Quality: defaultQuality},
{Name: "720p0", Width: 1280, Height: 720, Bitrate: 4_800_001, Quality: defaultQuality},
},
},
{
Expand All @@ -72,7 +72,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) {
},
want: []EncodedProfile{
{Name: "360p0", Width: 640, Height: 360, Bitrate: 320_000, Quality: defaultQuality},
{Name: "720p0", Width: 1200, Height: 720, Bitrate: 1_000_001, Quality: defaultQuality},
{Name: "720p0", Width: 1200, Height: 720, Bitrate: 1_200_001, Quality: defaultQuality},
},
},
{
Expand All @@ -88,7 +88,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) {
want: []EncodedProfile{
{Name: "360p0", Width: 640, Height: 360, Bitrate: 666_666, Quality: defaultQuality},
{Name: "720p0", Width: 1280, Height: 720, Bitrate: 2_666_666, Quality: defaultQuality},
{Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 5_000_000, Quality: defaultQuality},
{Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 6_000_000, Quality: defaultQuality},
},
},
{
Expand All @@ -103,7 +103,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) {
},
want: []EncodedProfile{
{Name: "low-bitrate", Width: 400, Height: 240, Bitrate: 258549, Quality: defaultQuality},
{Name: "240p0", Width: 400, Height: 240, Bitrate: 517099, Quality: defaultQuality},
{Name: "240p0", Width: 400, Height: 240, Bitrate: 620518, Quality: defaultQuality},
},
},
{
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) {
},
want: []EncodedProfile{
{Name: "360p0", Width: 640, Height: 360, Bitrate: 146_666, Quality: defaultQuality},
{Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 1_100_000, Quality: defaultQuality},
{Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 1_320_000, Quality: defaultQuality},
},
},
{
Expand All @@ -149,7 +149,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) {
},
want: []EncodedProfile{
{Name: "360p0", Width: 640, Height: 360, Bitrate: 146_666, Quality: defaultQuality},
{Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 1_100_000, Quality: defaultQuality},
{Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 1_320_000, Quality: defaultQuality},
},
},
}
Expand Down

0 comments on commit 13a6bf2

Please sign in to comment.