summaryrefslogtreecommitdiff
path: root/chromium/media/gpu/gpu_video_encode_accelerator_helpers.cc
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2022-05-12 15:59:20 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2022-05-25 06:57:22 +0000
commitf7eaed5286974984ba5f9e3189d8f49d03e99f81 (patch)
treecaed19b2af2024f35449fb0b781d0a25e09d4f8f /chromium/media/gpu/gpu_video_encode_accelerator_helpers.cc
parent9729c4479fe23554eae6e6dd1f30ff488f470c84 (diff)
downloadqtwebengine-chromium-f7eaed5286974984ba5f9e3189d8f49d03e99f81.tar.gz
BASELINE: Update Chromium to 100.0.4896.167
Change-Id: I98cbeb5d7543d966ffe04d8cefded0c493a11333 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/media/gpu/gpu_video_encode_accelerator_helpers.cc')
-rw-r--r--chromium/media/gpu/gpu_video_encode_accelerator_helpers.cc88
1 files changed, 88 insertions, 0 deletions
diff --git a/chromium/media/gpu/gpu_video_encode_accelerator_helpers.cc b/chromium/media/gpu/gpu_video_encode_accelerator_helpers.cc
index 52c0f85b38c..4e1ba46fbe7 100644
--- a/chromium/media/gpu/gpu_video_encode_accelerator_helpers.cc
+++ b/chromium/media/gpu/gpu_video_encode_accelerator_helpers.cc
@@ -8,9 +8,16 @@
#include "base/check_op.h"
#include "base/notreached.h"
+#include "base/numerics/safe_conversions.h"
namespace media {
namespace {
+// The maximum number of supported spatial layers and temporal layers. These
+// come from the maximum number of layers currently supported by
+// VideoEncodeAccelerator implementation.
+constexpr size_t kMaxSpatialLayers = 3;
+constexpr size_t kMaxTemporalLayers = 3;
+
// The maximum size for output buffer, which is chosen empirically for
// 1080p video.
constexpr size_t kMaxBitstreamBufferSizeInBytes = 2 * 1024 * 1024; // 2MB
@@ -51,6 +58,40 @@ size_t GetMaxEncodeBitstreamBufferSize(const gfx::Size& size) {
return kMaxBitstreamBufferSizeInBytes;
}
+VideoBitrateAllocation AllocateBitrateForDefaultEncodingWithBitrates(
+ const std::vector<uint32_t>& sl_bitrates,
+ const size_t num_temporal_layers) {
+ CHECK(!sl_bitrates.empty());
+ CHECK_LE(sl_bitrates.size(), kMaxSpatialLayers);
+
+ // The same bitrate factors as the software encoder.
+ // https://source.chromium.org/chromium/chromium/src/+/main:media/video/vpx_video_encoder.cc;l=131;drc=d383d0b3e4f76789a6de2a221c61d3531f4c59da
+ constexpr double kTemporalLayersBitrateScaleFactors[][kMaxTemporalLayers] = {
+ {1.00, 0.00, 0.00}, // For one temporal layer.
+ {0.60, 0.40, 0.00}, // For two temporal layers.
+ {0.50, 0.20, 0.30}, // For three temporal layers.
+ };
+
+ CHECK_GT(num_temporal_layers, 0u);
+ CHECK_LE(num_temporal_layers, base::size(kTemporalLayersBitrateScaleFactors));
+ DCHECK_EQ(base::size(kTemporalLayersBitrateScaleFactors), kMaxTemporalLayers);
+
+ VideoBitrateAllocation bitrate_allocation;
+ for (size_t spatial_id = 0; spatial_id < sl_bitrates.size(); ++spatial_id) {
+ const uint32_t bitrate_bps = sl_bitrates[spatial_id];
+ for (size_t temporal_id = 0; temporal_id < num_temporal_layers;
+ ++temporal_id) {
+ const double factor =
+ kTemporalLayersBitrateScaleFactors[num_temporal_layers - 1]
+ [temporal_id];
+ bitrate_allocation.SetBitrate(
+ spatial_id, temporal_id,
+ base::saturated_cast<uint32_t>(bitrate_bps * factor));
+ }
+ }
+
+ return bitrate_allocation;
+}
} // namespace
size_t GetEncodeBitstreamBufferSize(const gfx::Size& size,
@@ -113,4 +154,51 @@ std::vector<uint8_t> GetFpsAllocation(size_t num_temporal_layers) {
}
}
+VideoBitrateAllocation AllocateBitrateForDefaultEncoding(
+ const VideoEncodeAccelerator::Config& config) {
+ if (config.spatial_layers.empty()) {
+ return AllocateBitrateForDefaultEncodingWithBitrates(
+ {config.bitrate.target()},
+ /*num_temporal_layers=*/1u);
+ }
+
+ const size_t num_temporal_layers =
+ config.spatial_layers[0].num_of_temporal_layers;
+ std::vector<uint32_t> bitrates;
+ bitrates.reserve(config.spatial_layers.size());
+ for (const auto& spatial_layer : config.spatial_layers) {
+ DCHECK_EQ(spatial_layer.num_of_temporal_layers, num_temporal_layers);
+ bitrates.push_back(spatial_layer.bitrate_bps);
+ }
+
+ return AllocateBitrateForDefaultEncodingWithBitrates(bitrates,
+ num_temporal_layers);
+}
+
+VideoBitrateAllocation AllocateDefaultBitrateForTesting(
+ const size_t num_spatial_layers,
+ const size_t num_temporal_layers,
+ const uint32_t bitrate) {
+ // Higher spatial layers (those to the right) get more bitrate.
+ constexpr double kSpatialLayersBitrateScaleFactors[][kMaxSpatialLayers] = {
+ {1.00, 0.00, 0.00}, // For one spatial layer.
+ {0.30, 0.70, 0.00}, // For two spatial layers.
+ {0.07, 0.23, 0.70}, // For three spatial layers.
+ };
+
+ CHECK_GT(num_spatial_layers, 0u);
+ CHECK_LE(num_spatial_layers, base::size(kSpatialLayersBitrateScaleFactors));
+ DCHECK_EQ(base::size(kSpatialLayersBitrateScaleFactors), kMaxSpatialLayers);
+
+ std::vector<uint32_t> bitrates(num_spatial_layers);
+ for (size_t sid = 0; sid < num_spatial_layers; ++sid) {
+ const double bitrate_factor =
+ kSpatialLayersBitrateScaleFactors[num_spatial_layers - 1][sid];
+ bitrates[sid] = bitrate * bitrate_factor;
+ }
+
+ return AllocateBitrateForDefaultEncodingWithBitrates(bitrates,
+ num_temporal_layers);
+}
+
} // namespace media