1 /*
2  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/video_coding/utility/default_video_bitrate_allocator.h"
12 
13 #include <stdint.h>
14 
15 namespace webrtc {
16 
DefaultVideoBitrateAllocator(const VideoCodec & codec)17 DefaultVideoBitrateAllocator::DefaultVideoBitrateAllocator(
18     const VideoCodec& codec)
19     : codec_(codec) {}
20 
~DefaultVideoBitrateAllocator()21 DefaultVideoBitrateAllocator::~DefaultVideoBitrateAllocator() {}
22 
GetAllocation(uint32_t total_bitrate_bps,uint32_t framerate)23 BitrateAllocation DefaultVideoBitrateAllocator::GetAllocation(
24     uint32_t total_bitrate_bps,
25     uint32_t framerate) {
26   BitrateAllocation allocation;
27   if (total_bitrate_bps == 0)
28     return allocation;
29 
30   if (total_bitrate_bps < codec_.minBitrate * 1000) {
31     allocation.SetBitrate(0, 0, codec_.minBitrate * 1000);
32   } else if (codec_.maxBitrate > 0 &&
33              total_bitrate_bps > codec_.maxBitrate * 1000) {
34     allocation.SetBitrate(0, 0, codec_.maxBitrate * 1000);
35   } else {
36     allocation.SetBitrate(0, 0, total_bitrate_bps);
37   }
38   return allocation;
39 }
40 
GetPreferredBitrateBps(uint32_t framerate)41 uint32_t DefaultVideoBitrateAllocator::GetPreferredBitrateBps(
42     uint32_t framerate) {
43   return GetAllocation(codec_.maxBitrate * 1000, framerate).get_sum_bps();
44 }
45 
46 }  // namespace webrtc
47