blob: 4a7f07a443b4c64a969190df711f1a24b13978aa [file] [log] [blame]
Danil Chapovalov02b17a52020-02-05 14:06:171/*
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/frame_dependencies_calculator.h"
11
12#include <stdint.h>
13
Philipp Hanckea49e3342025-04-07 21:49:2014#include <cstddef>
Danil Chapovalov02b17a52020-02-05 14:06:1715#include <iterator>
Philipp Hanckea49e3342025-04-07 21:49:2016#include <optional>
Danil Chapovalov02b17a52020-02-05 14:06:1717#include <set>
18
19#include "absl/algorithm/container.h"
20#include "absl/container/inlined_vector.h"
21#include "api/array_view.h"
Philipp Hanckea49e3342025-04-07 21:49:2022#include "common_video/generic_frame_descriptor/generic_frame_info.h"
Danil Chapovalov02b17a52020-02-05 14:06:1723#include "rtc_base/checks.h"
24#include "rtc_base/logging.h"
Danil Chapovalov02b17a52020-02-05 14:06:1725
26namespace webrtc {
27
28absl::InlinedVector<int64_t, 5> FrameDependenciesCalculator::FromBuffersUsage(
Danil Chapovalov02b17a52020-02-05 14:06:1729 int64_t frame_id,
30 rtc::ArrayView<const CodecBufferUsage> buffers_usage) {
Danil Chapovalov02b17a52020-02-05 14:06:1731 absl::InlinedVector<int64_t, 5> dependencies;
32 RTC_DCHECK_GT(buffers_usage.size(), 0);
33 for (const CodecBufferUsage& buffer_usage : buffers_usage) {
34 RTC_CHECK_GE(buffer_usage.id, 0);
35 if (buffers_.size() <= static_cast<size_t>(buffer_usage.id)) {
36 buffers_.resize(buffer_usage.id + 1);
37 }
38 }
39 std::set<int64_t> direct_depenendencies;
40 std::set<int64_t> indirect_depenendencies;
Danil Chapovalovcf1308f2020-11-18 17:27:3741
42 for (const CodecBufferUsage& buffer_usage : buffers_usage) {
43 if (!buffer_usage.referenced) {
44 continue;
Danil Chapovalov02b17a52020-02-05 14:06:1745 }
Danil Chapovalovcf1308f2020-11-18 17:27:3746 const BufferUsage& buffer = buffers_[buffer_usage.id];
Florent Castelli8037fc62024-08-29 13:00:4047 if (buffer.frame_id == std::nullopt) {
Danil Chapovalovcf1308f2020-11-18 17:27:3748 RTC_LOG(LS_ERROR) << "Odd configuration: frame " << frame_id
49 << " references buffer #" << buffer_usage.id
50 << " that was never updated.";
51 continue;
52 }
53 direct_depenendencies.insert(*buffer.frame_id);
54 indirect_depenendencies.insert(buffer.dependencies.begin(),
55 buffer.dependencies.end());
Danil Chapovalov02b17a52020-02-05 14:06:1756 }
Danil Chapovalovcf1308f2020-11-18 17:27:3757 // Reduce references: if frame #3 depends on frame #2 and #1, and frame #2
58 // depends on frame #1, then frame #3 needs to depend just on frame #2.
59 // Though this set diff removes only 1 level of indirection, it seems
60 // enough for all currently used structures.
61 absl::c_set_difference(direct_depenendencies, indirect_depenendencies,
62 std::back_inserter(dependencies));
Danil Chapovalov02b17a52020-02-05 14:06:1763
64 // Update buffers.
65 for (const CodecBufferUsage& buffer_usage : buffers_usage) {
66 if (!buffer_usage.updated) {
67 continue;
68 }
69 BufferUsage& buffer = buffers_[buffer_usage.id];
70 buffer.frame_id = frame_id;
71 buffer.dependencies.assign(direct_depenendencies.begin(),
72 direct_depenendencies.end());
73 }
74
75 return dependencies;
76}
77
78} // namespace webrtc