scalability_structure_test_helpers.cc (3571B)
1 /* 2 * Copyright (c) 2020 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 #include "modules/video_coding/svc/scalability_structure_test_helpers.h" 11 12 #include <bitset> 13 #include <cstddef> 14 #include <cstdint> 15 #include <utility> 16 #include <vector> 17 18 #include "api/array_view.h" 19 #include "api/video/video_bitrate_allocation.h" 20 #include "common_video/generic_frame_descriptor/generic_frame_info.h" 21 #include "modules/video_coding/chain_diff_calculator.h" 22 #include "modules/video_coding/frame_dependencies_calculator.h" 23 #include "modules/video_coding/svc/scalable_video_controller.h" 24 #include "test/gtest.h" 25 26 namespace webrtc { 27 28 VideoBitrateAllocation EnableTemporalLayers(int s0, int s1, int s2) { 29 VideoBitrateAllocation bitrate; 30 for (int tid = 0; tid < s0; ++tid) { 31 bitrate.SetBitrate(0, tid, 1'000'000); 32 } 33 for (int tid = 0; tid < s1; ++tid) { 34 bitrate.SetBitrate(1, tid, 1'000'000); 35 } 36 for (int tid = 0; tid < s2; ++tid) { 37 bitrate.SetBitrate(2, tid, 1'000'000); 38 } 39 return bitrate; 40 } 41 42 void ScalabilityStructureWrapper::GenerateFrames( 43 int num_temporal_units, 44 std::vector<GenericFrameInfo>& frames) { 45 for (int i = 0; i < num_temporal_units; ++i) { 46 for (auto& layer_frame : 47 structure_controller_.NextFrameConfig(/*restart=*/false)) { 48 int64_t frame_id = ++frame_id_; 49 bool is_keyframe = layer_frame.IsKeyframe(); 50 51 GenericFrameInfo frame_info = 52 structure_controller_.OnEncodeDone(layer_frame); 53 if (is_keyframe) { 54 chain_diff_calculator_.Reset(frame_info.part_of_chain); 55 } 56 frame_info.chain_diffs = 57 chain_diff_calculator_.From(frame_id, frame_info.part_of_chain); 58 for (int64_t base_frame_id : frame_deps_calculator_.FromBuffersUsage( 59 frame_id, frame_info.encoder_buffers)) { 60 frame_info.frame_diffs.push_back(frame_id - base_frame_id); 61 } 62 63 frames.push_back(std::move(frame_info)); 64 } 65 } 66 } 67 68 bool ScalabilityStructureWrapper::FrameReferencesAreValid( 69 ArrayView<const GenericFrameInfo> frames) const { 70 bool valid = true; 71 // VP9 and AV1 supports up to 8 buffers. Expect no more buffers are not used. 72 std::bitset<8> buffer_contains_frame; 73 for (size_t i = 0; i < frames.size(); ++i) { 74 const GenericFrameInfo& frame = frames[i]; 75 for (const CodecBufferUsage& buffer_usage : frame.encoder_buffers) { 76 if (buffer_usage.id < 0 || buffer_usage.id >= 8) { 77 ADD_FAILURE() << "Invalid buffer id " << buffer_usage.id 78 << " for frame#" << i 79 << ". Up to 8 buffers are supported."; 80 valid = false; 81 continue; 82 } 83 if (buffer_usage.referenced && !buffer_contains_frame[buffer_usage.id]) { 84 ADD_FAILURE() << "buffer " << buffer_usage.id << " for frame#" << i 85 << " was reference before updated."; 86 valid = false; 87 } 88 if (buffer_usage.updated) { 89 buffer_contains_frame.set(buffer_usage.id); 90 } 91 } 92 for (int fdiff : frame.frame_diffs) { 93 if (fdiff <= 0 || static_cast<size_t>(fdiff) > i) { 94 ADD_FAILURE() << "Invalid frame diff " << fdiff << " for frame#" << i; 95 valid = false; 96 } 97 } 98 } 99 return valid; 100 } 101 102 } // namespace webrtc