File: corruption_detection_converters.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (139 lines) | stat: -rw-r--r-- 5,154 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 *  Copyright 2024 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "common_video/corruption_detection_converters.h"

#include <optional>
#include <vector>

#include "api/transport/rtp/corruption_detection_message.h"
#include "common_video/frame_instrumentation_data.h"
#include "rtc_base/checks.h"

namespace webrtc {

namespace {

int GetFullSequenceIndex(int previous_sequence_index,
                         int sequence_index_update,
                         bool update_the_most_significant_bits) {
  RTC_CHECK_GE(previous_sequence_index, 0)
      << "previous_sequence_index must not be negative";
  RTC_CHECK_LE(previous_sequence_index, 0x7FFF)
      << "previous_sequence_index must be at most 15 bits";
  RTC_CHECK_GE(sequence_index_update, 0)
      << "sequence_index_update must not be negative";
  RTC_CHECK_LE(sequence_index_update, 0b0111'1111)
      << "sequence_index_update must be at most 7 bits";
  if (update_the_most_significant_bits) {
    // Reset LSB.
    return sequence_index_update << 7;
  }
  int upper_bits = previous_sequence_index & 0b0011'1111'1000'0000;
  if (sequence_index_update < (previous_sequence_index & 0b0111'1111)) {
    // Assume one and only one wraparound has happened.
    upper_bits += 0b1000'0000;
  }
  // Replace the lowest bits with the bits from the update.
  return upper_bits + sequence_index_update;
}

int GetSequenceIndexForMessage(int sequence_index,
                               bool communicate_upper_bits) {
  return communicate_upper_bits ? (sequence_index >> 7)
                                : (sequence_index & 0b0111'1111);
}

}  // namespace

std::optional<FrameInstrumentationData>
ConvertCorruptionDetectionMessageToFrameInstrumentationData(
    const CorruptionDetectionMessage& message,
    int previous_sequence_index) {
  if (previous_sequence_index < 0) {
    return std::nullopt;
  }
  if (message.sample_values().empty()) {
    return std::nullopt;
  }
  int full_sequence_index = GetFullSequenceIndex(
      previous_sequence_index, message.sequence_index(),
      message.interpret_sequence_index_as_most_significant_bits());
  std::vector<double> sample_values(message.sample_values().cbegin(),
                                    message.sample_values().cend());
  return FrameInstrumentationData{
      .sequence_index = full_sequence_index,
      .communicate_upper_bits =
          message.interpret_sequence_index_as_most_significant_bits(),
      .std_dev = message.std_dev(),
      .luma_error_threshold = message.luma_error_threshold(),
      .chroma_error_threshold = message.chroma_error_threshold(),
      .sample_values = sample_values};
}

std::optional<FrameInstrumentationSyncData>
ConvertCorruptionDetectionMessageToFrameInstrumentationSyncData(
    const CorruptionDetectionMessage& message,
    int previous_sequence_index) {
  if (previous_sequence_index < 0) {
    return std::nullopt;
  }
  if (!message.sample_values().empty()) {
    return std::nullopt;
  }
  if (!message.interpret_sequence_index_as_most_significant_bits()) {
    return std::nullopt;
  }
  return FrameInstrumentationSyncData{
      .sequence_index = GetFullSequenceIndex(
          previous_sequence_index, message.sequence_index(),
          /*update_the_most_significant_bits=*/true),
      .communicate_upper_bits = true};
}

std::optional<CorruptionDetectionMessage>
ConvertFrameInstrumentationDataToCorruptionDetectionMessage(
    const FrameInstrumentationData& data) {
  if (data.sequence_index < 0 || data.sequence_index > 0b0011'1111'1111'1111) {
    return std::nullopt;
  }
  // Frame instrumentation data must have sample values.
  if (data.sample_values.empty()) {
    return std::nullopt;
  }
  return CorruptionDetectionMessage::Builder()
      .WithSequenceIndex(GetSequenceIndexForMessage(
          data.sequence_index, data.communicate_upper_bits))
      .WithInterpretSequenceIndexAsMostSignificantBits(
          data.communicate_upper_bits)
      .WithStdDev(data.std_dev)
      .WithLumaErrorThreshold(data.luma_error_threshold)
      .WithChromaErrorThreshold(data.chroma_error_threshold)
      .WithSampleValues(data.sample_values)
      .Build();
}

std::optional<CorruptionDetectionMessage>
ConvertFrameInstrumentationSyncDataToCorruptionDetectionMessage(
    const FrameInstrumentationSyncData& data) {
  RTC_DCHECK(data.communicate_upper_bits)
      << "FrameInstrumentationSyncData data must always send the upper bits.";

  if (data.sequence_index < 0 || data.sequence_index > 0b0011'1111'1111'1111) {
    return std::nullopt;
  }
  return CorruptionDetectionMessage::Builder()
      .WithSequenceIndex(GetSequenceIndexForMessage(
          data.sequence_index, data.communicate_upper_bits))
      .WithInterpretSequenceIndexAsMostSignificantBits(true)
      .Build();
}

}  // namespace webrtc