File: reorder_optimizer.cc

package info (click to toggle)
firefox 142.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,591,884 kB
  • sloc: cpp: 7,451,570; javascript: 6,392,463; ansic: 3,712,584; python: 1,388,569; xml: 629,223; asm: 426,919; java: 184,857; sh: 63,439; makefile: 19,150; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (77 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download | duplicates (3)
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
/*
 *  Copyright (c) 2021 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 "modules/audio_coding/neteq/reorder_optimizer.h"

#include <stdint.h>

#include <algorithm>
#include <limits>
#include <vector>

namespace webrtc {

namespace {

constexpr int kDelayBuckets = 100;
constexpr int kBucketSizeMs = 20;

}  // namespace

ReorderOptimizer::ReorderOptimizer(int forget_factor,
                                   int ms_per_loss_percent,
                                   std::optional<int> start_forget_weight)
    : histogram_(kDelayBuckets, forget_factor, start_forget_weight),
      ms_per_loss_percent_(ms_per_loss_percent) {}

void ReorderOptimizer::Update(int relative_delay_ms,
                              bool reordered,
                              int base_delay_ms) {
  const int index = reordered ? relative_delay_ms / kBucketSizeMs : 0;
  if (index < histogram_.NumBuckets()) {
    // Maximum delay to register is 2000 ms.
    histogram_.Add(index);
  }
  int bucket_index = MinimizeCostFunction(base_delay_ms);
  optimal_delay_ms_ = (1 + bucket_index) * kBucketSizeMs;
}

void ReorderOptimizer::Reset() {
  histogram_.Reset();
  optimal_delay_ms_.reset();
}

int ReorderOptimizer::MinimizeCostFunction(int base_delay_ms) const {
  const std::vector<int>& buckets = histogram_.buckets();

  // Values are calculated in Q30.
  int64_t loss_probability = 1 << 30;
  int64_t min_cost = std::numeric_limits<int64_t>::max();
  int min_bucket = 0;
  for (int i = 0; i < static_cast<int>(buckets.size()); ++i) {
    loss_probability -= buckets[i];
    int64_t delay_ms =
        static_cast<int64_t>(std::max(0, i * kBucketSizeMs - base_delay_ms))
        << 30;
    int64_t cost = delay_ms + 100 * ms_per_loss_percent_ * loss_probability;

    if (cost < min_cost) {
      min_cost = cost;
      min_bucket = i;
    }
    if (loss_probability == 0) {
      break;
    }
  }

  return min_bucket;
}

}  // namespace webrtc