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